首先,因为这是我的第一篇文章,向这个社区的每个人致敬。我已经找到了大量的信息来帮助我解决迄今为止遇到的问题。现在我遇到了一个我无法解决的问题。我正在制作一个“Pang”类型的游戏,可能你们中的一些人已经看过或玩过某种变体。所以说重点。我目前正在尝试制作一个与课程相关联的对象,在舞台上弹跳。我决定尝试使用 easeIn 和 easeOut 补间。
我在主文档类中声明补间:
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);
}
}
现在,当我尝试从 Bubble.as 类内部启动补间时,我得到一个空对象引用。
也许更多信息会有所帮助。我在主类中的公共函数中实例化对象,如下所示:
public function makeBubble(size:Number, xCoordinate:Number, yCoordinate:Number, xDir:Number):void
{
if (size == 1)
{
bubble = new Bubble(stage);
bubble.x = xCoordinate;
bubble.y = yCoordinate;
bubble.xDirection = xDir;
bubbles1.push(bubble);
stage.addChild(bubble);
}
这是完整的 Bubble.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);
}
}
}