Actionscript 3 尝试启动tween返回空对象引用
首先,因为这是我的第一篇帖子,向这个社区的每个人致敬。我已经找到了大量的信息来帮助我解决目前遇到的问题。现在我遇到了一个我无法解决的问题。我正在做一个剧痛类型的游戏,也许你们中的一些人已经看过或玩过某种变化。说到点子上。我目前正试图让一个链接到类的对象在舞台上反弹。我决定尝试使用easeIn和easeOut tweens 我在主文档类中声明tweens:Actionscript 3 尝试启动tween返回空对象引用,actionscript-3,Actionscript 3,首先,因为这是我的第一篇帖子,向这个社区的每个人致敬。我已经找到了大量的信息来帮助我解决目前遇到的问题。现在我遇到了一个我无法解决的问题。我正在做一个剧痛类型的游戏,也许你们中的一些人已经看过或玩过某种变化。说到点子上。我目前正试图让一个链接到类的对象在舞台上反弹。我决定尝试使用easeIn和easeOut tweens 我在主文档类中声明tweens: public static var upTween:Tween; public static var downTween:Tween;
public static var upTween:Tween;
public static var downTween:Tween;
并使用for循环将其值指定给数组中的所有对象:
public function bounce(event:Event):void
{
for (var i:uint = 0; i < bubbles1.length; i++)
{
upTween = new Tween(bubbles1[i], "y", Strong.easeOut, bubbles1[i].y, 250, 2, true);
downTween = new Tween(bubbles1[i], "y", Strong.easeIn, bubbles1[i].y, stage.stageHeight - 50 - bubbles1[i].height, 2, true);
}
}
下面是完整的气泡。as类:
package com.zdravko.pong
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class Bubble extends MovieClip
{
private var stageRef:Stage;
var xDirection:Number;
var yDirection:Number;
var bubble2:Bubble2;
public function Bubble(stageRef:Stage)
{
// constructor code
this.stageRef = stageRef;
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
function loop(event:Event):void
{
if(this.x >= stage.stageWidth - this.width)
{
this.x = stage.stageWidth - this.width - 5;
xDirection *= -1;
}
if(this.x <= 0)
{
this.x = 5;
xDirection *= -1;
}
if(this.y >= stage.stageHeight - 50 - this.height)
{
this.y = stage.stageHeight - 50 - this.height;
Engine.upTween.start();
}
if(this.y <= 250)
{
this.y = 250;
Engine.downTween.start();
}
this.x += xDirection;
if(hitTestObject(Engine.player) && Player.invul == false)
{
decreaseEnergy(.4);
Player.invul = true;
Player.invulTimer.start();
}
}
public function decreaseEnergy(dmg:Number):void
{
Engine.energy.scaleX -= dmg;
}
public function takeHit() : void
{
makeBubble(2, this.x + 50, this.y + 30, 8, 8);
makeBubble(2, this.x - 20, this.y - 30, -8, 8);
removeSelf();
Engine.playerScore += 500;
Engine.score.scoreBox.text = Engine.playerScore;
}
private function removeSelf() : void
{
removeEventListener(Event.ENTER_FRAME, loop);
if (stageRef.contains(this))
{
stageRef.removeChild(this);
}
}
private function makeBubble(size:Number, xCoordinate:Number, yCoordinate:Number, xDir:Number, yDir:Number):void
{
bubble2 = new Bubble2(stage);
bubble2.x = xCoordinate;
bubble2.y = yCoordinate;
bubble2.xDirection = xDir;
bubble2.yDirection = yDir;
Engine.bubbles2.push(bubble2);
stageRef.addChild(bubble2);
}
}
}推测气泡数组正在返回空值。您是否尝试过从bubbles1数组中找出值
public function bounce(event:Event):void
{
for (var i:uint = 0; i < bubbles1.length; i++)
{
// log out the value from your array
trace("bubble " + bubbles1[i]);
}
}
我就这么做了,在我试着开始tween的线路上添加了traceEngine.bubbles1.length;它确实应该返回1,好的。我建议跟踪数组中的值,而不是长度,以检查值是否为nulltryed,这是您在edditing后建议的。相同的结果:{public function bounceevent:Event:void}在什么时候获得calledIt从主类构造函数hmm接收enter_frame事件。为什么循环使用静态变量?你看,如果你有20个泡泡,你需要40个tween对象,而你只有2个。也许你观察到的是一个架构错误,不在这个特定的地方。我很确定有更好的方法来做到这一点,我只是想不出一个。我得到的错误是错误1009:无法访问空对象引用的属性或方法。如果我拆下发动机,则启动;从Bubble.as类中删除行,错误消失。
public function bounce(event:Event):void
{
for (var i:uint = 0; i < bubbles1.length; i++)
{
// log out the value from your array
trace("bubble " + bubbles1[i]);
}
}