Adding child, creating a tween and removing the child

Want the animation to play before the object is removed

Page 1 of 1

6 Replies - 3385 Views - Last Post: 08 January 2010 - 08:24 PM

#1 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Adding child, creating a tween and removing the child

Posted 06 January 2010 - 03:51 PM

Hello all,

I am currently a programming student and am attempting to learn some Flash/Action Script in my spare time for fun. I have been familierizing myself with the language and it's application using Flash. I have started to make a simple game to implement what I have learned so far and have run into a snag.

What I am doing is instantiating a fist image, increasing its height and width to simulate it coming towards the screen as a punch(yes I know lame but it's a start :P ). After the punch is thrown I wanted to remove the fist from the stage. My problem is that the remove tween code is ran instantly and the animation does not have time to play. This is the code I am using :



import fl.transitions.Tween;
import fl.transitions.easing.*;

var fist = new Fist;

fist.x = 50;
fist.y = 50;

addChild(fist);

var fistTween:Tween = new Tween(fist,"width",None.easeNone,50,100,2,true);
var fistTween2:Tween=new Tween(fist,"height",None.easeNone,50,100,2,true);

removeChild(fist);




I would of course wrap most of that up in a punch method. Could anyone advise me on how I could make the line to remove the child not run until after the animation completes? Any info would be appreciated, thanks much!

Is This A Good Question/Topic? 0
  • +

Replies To: Adding child, creating a tween and removing the child

#2 maxrock  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 70
  • Joined: 13-September 09

Re: Adding child, creating a tween and removing the child

Posted 06 January 2010 - 08:50 PM

Try putting a Event Listener on your tween for "TweenEvent.MOTION_FINISH" :)
Was This Post Helpful? 0
  • +
  • -

#3 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Adding child, creating a tween and removing the child

Posted 07 January 2010 - 09:18 AM

View Postmaxrock, on 6 Jan, 2010 - 07:50 PM, said:

Try putting a Event Listener on your tween for "TweenEvent.MOTION_FINISH" :)


Thanks much for the reply Maxrock!,

I have taken your suggestion but have run into another problem. My thought was to add the event listener that would call a function that would remove the child from the stage(the fist object). I get the following error :

"The supplied DisplayObject must be a child of the caller"

I sort of understand what it means as I am familiarizing myself with the display list hierarchy. I am wondering how I would be able to remove a child from the stage(or anywhere) in a function that does not originally add the child. Here is the code I used :

function punch():void
{
	stage.removeEventListener(Event.ENTER_FRAME,moveEarl);
	var fist = new Fist;

fist.x = 100;
fist.y = 100;

stage.addChild(fist);
var fistTween:Tween = new Tween(fist,"width",None.easeNone,50,100,1,true);
var fistTween2:Tween = new Tween(fist,"height",None.easeNone,50,100,1,true);
fistTween.addEventListener(TweenEvent.MOTION_FINISH,endPunch);

}

function endPunch(e:Event):void
{
	stage.removeChild(fist);
	
}





I know the event listener works because the error does not occur until after the animation is complete. Again thanks for the help, any more advice on how to proceed here would be very much appreciated!

Jstall
Was This Post Helpful? 0
  • +
  • -

#4 maxrock  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 70
  • Joined: 13-September 09

Re: Adding child, creating a tween and removing the child

Posted 07 January 2010 - 12:26 PM

I'm not sure about this one.

My ideas:

1.Check whether "fist" is accessible to endPunch function.

2.try using fist.parent.removeChild(fist);
Was This Post Helpful? 0
  • +
  • -

#5 maxrock  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 70
  • Joined: 13-September 09

Re: Adding child, creating a tween and removing the child

Posted 07 January 2010 - 12:36 PM

Check this as well :)
http://senocular.com...imeerrors/#2025

This post has been edited by maxrock: 07 January 2010 - 12:36 PM

Was This Post Helpful? 1
  • +
  • -

#6 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Adding child, creating a tween and removing the child

Posted 08 January 2010 - 01:40 PM

View Postmaxrock, on 7 Jan, 2010 - 11:26 AM, said:

I'm not sure about this one.

My ideas:

1.Check whether "fist" is accessible to endPunch function.

2.try using fist.parent.removeChild(fist);



Ahh yes, got it, it was a scope problem. The fist object was instantiated in the first method so was not available to the second. I, for some reason, thought that once something was added to the stage it was globally accessible. Just changed the scope of the variable and it works fine. Thanks so much for the help, it is appreciated!
Was This Post Helpful? 0
  • +
  • -

#7 maxrock  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 70
  • Joined: 13-September 09

Re: Adding child, creating a tween and removing the child

Posted 08 January 2010 - 08:24 PM

Welcome :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1