PDA

View Full Version : Can new created objects on stage be set as tween objects using tween animation?


Captain Jonesy
04-12-2011, 12:23 PM
Hi guys,

I'm currently making my own 2D shoot' em up and I've created a simple enemy spawn state machine which allows the game to automatically keep spawning/creating new enemies on the stage.

Before I had this spawn loop machine/code set up, I had only one single enemy which was already on the stage to test the gameplay of my game and the enemy as set up tween object using tween animation. I'm wondering if its possible to add/able tween animation on all new created enemies as appear on the stage giving them movement/ai? This is possible?

I've experimented with some code, I'm failed to succeed withs such functionality, I just wondering where I am going wrong or if anyone knew of any alternatives to my approach?

public function makeEnemies():void{
for(var i:int = 0; i < level; i++){/// creates a enemy for every level
var tempEnemy:Enemy = new Enemy();
addChild(tempEnemy);
enemies.push(tempEnemy);
tempEnemy.gotoAndStop(1);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
tempEnemy.addEventListener(Event.ENTER_FRAME, onEnemyMove);

_tweenX = new Tween(tempEnemy, "x", Regular.easeInOut, tempEnemy.x, _randomX, 45, false);
_tweenY = new Tween(tempEnemy, "y", Regular.easeInOut, tempEnemy.y, _randomY, 45, false)
_tweenX.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinishX);
_tweenY.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinishY);
}
function onMotionFinishX(event:Event){
var tempEnemy:Enemy = new Enemy();
var tweenObject:Tween = TempEnemy(event.currentTarget);
//Calculate new start and finish positions
tweenObject.begin = enemy.x;
tweenObject.finish = Math.ceil(Math.random() * 400);
tweenObject.finish = player.x;
//Start the Tween object playing again
tweenObject.start();
}
function onMotionFinishY(event:Event){
var tweenObject:Tween = tempEnemy(event.currentTarget as Tween);
tweenObject.begin = enemy.y;
tweenObject.finish = Math.ceil(Math.random() * 400);
tweenObject.finish = player.y;
tweenObject.start();}

It doesn't seem to like this line of code - var tweenObject:Tween = tempEnemy(event.currentTarget as Tween) - either I get a error saying that tempEnemy is undefined method or if I change it to Enemy instead of tempEnemy I get a error 1067: Implicat coercion of value of type Enemy to an unrelated type.fl.transitions:Tween.

Does this line make sense? Or am I'm going into the wrong direction? Thank you for your time and reading all that and any help/suggestions would be greatly appreciated.

Thanks

Jonesy