08-06-2012, 02:16 AM
|
#1
|
|
Registered User
Join Date: Aug 2012
Posts: 6
|
Tween, fading every sprite after movement.
I wanted to create some "particle generator" with circles.
I met a problem with this "event handlers" in ActionScript 3
When I was working with AS 2 it was enought to write "onMotionFinished" in function, where particles was creating SO my link to that particular particle will go through "onMotionFinished" and get all the things done to it (like fading and deleting itself to free memory).
Not I've got a problem "how to say in event handler which particle needs to dissapear".
There is part of my code:
ActionScript Code:
function behave(i:Sprite, groundy:Number, howlongfly:Number, howlongdrop2:Number)
{
var _fly:Tween = new Tween(i,"x",Strong.easeOut,i.x,i.x + howlongfly,2,true);
var drop:Tween = new Tween(i,"y",Bounce.easeOut,i.y,groundy,howlongdrop2,true);
_fly.addEventListener(TweenEvent.MOTION_FINISH, dissapear);
}
function dissapear(n:TweenEvent) {
//here code making i's alpha 0 with enter frame (btw this one I don't know too, how to make this here)
}
In AS2 I was going to do like
ActionScript Code:
function behave(i:Sprite, groundy:Number, howlongfly:Number, howlongdrop2:Number)
{
var _fly:Tween = new Tween(i,"x",Strong.easeOut,i.x,i.x + howlongfly,2,true);
var drop:Tween = new Tween(i,"y",Bounce.easeOut,i.y,groundy,howlongdrop2,true);
_fly.onMotionFinished = function {
i.onEnterFrame = function () {
this._alpha-=10;
if (this._alpha<=0) {
//here I remove this clip/sprite
}
}
}
}
Basically I had this linkage i:Object in AS2 because all this code was within one function. Now with handlers how can I give this i:Object to function "dissapear"? And then for example to function "delete" (after alpha=0)?
I may be newbie in AS 3, not knowing basics (now reading book of Colin Moock about it).
Thank you everyone who knows some good tip and advice
|
|
|
08-06-2012, 04:30 AM
|
#2
|
|
Super Saiyan
Join Date: Nov 2011
Location: New Zealand
Posts: 299
|
while this may not be the best approach, but it is probably what you're after. Instead of each object (in this case, sprite) having 'functions' that you create to do something, instead they fire "events" signalling when something is happening. So, your onEnterFrame function, which pretty much stays the same, is now accessed a little differently.
I have never used tween events, so, I'm just going to assume the MOTION_FINISHED event works.
ActionScript Code:
_fly.addEventListener(TweenEvent.MOTION_FINISH, dissapear);
function dissapear(twnEvt:TweenEvent):void
{
//this gets the sprite that the event happened on -- this may be different as I haven't used tween events
var me:Sprite = Sprite(twnEvt.currentTarget);
me.removeEventListener(TweenEvent.MOTION_FINISH, dissapear); //remove the tween finish listener
me.addEventListener(Event.ENTER_FRAME, spriteFadeOut); //this is going to be the new onEnterFrame function
}
function spriteFadeOut(evt:Event):void
{
//get the sprite from the event data given.
var me:Sprite = Sprite(evt.currentTarget);
if (me.alpha > 0)
{
me.alpha -= 0.1;
me.parent.removeChild(me); //remove it off the stage when done
}
else
{
me.removeEventListener(Event.ENTER_FRAME, spriteFadeOut); //remove this when it's done
}
}
I was short on time writing this, if you need more help or don't understand, just ask.
|
|
|
08-06-2012, 07:27 AM
|
#3
|
|
Registered User
Join Date: Aug 2012
Posts: 6
|
First of all, thank you for your answer.
But, undortunately, that didn't help.
I'll try to explain Tween class. The thing is that Tween is cool for changing parametres, vars values. But in my case (mostly in all common cases) it's changing x,y,scale and etc parametres.
For in-code animation. So basically when I'm gettin currentTarget after listener in new function, it will return Tween, not Sprite. Because it was Tween class and its event handler MOTION_FINISH.
That's the problem I'm talking about - after trigger MOTION_FINISH - how can I transport my var i, that is Sprite, from function "behave" (in op code) to function "dissapear"?
I think the problem is with Tween and it's event handler, indeed. Or I'm too newb in Tween and listeners/event handlers or my case is kinda hard to solve.
|
|
|
08-06-2012, 07:37 AM
|
#4
|
|
Senior Member
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 1,887
|
If you like the Tween class, you'll love Greensock TweenMax and TimelineMax.
Seriously, these classes are awesome for building particle systems and complex sequences. (and no GC issues like the Adobe Tween class)
|
|
|
08-06-2012, 07:55 AM
|
#5
|
|
Registered User
Join Date: Aug 2012
Posts: 6
|
Thank you, but first of all I must figure out how to solve this problem with handlers and tweens
|
|
|
08-06-2012, 12:30 PM
|
#6
|
|
Registered User
Join Date: Aug 2012
Posts: 6
|
Ok here's a full code for you to understand guys.
For optimizing I must delete particles when their movement stopped.
Full code which you can test in program and see what I mean.
ActionScript Code:
import fl.transitions.*;
import fl.transitions.easing.*;
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Sprite;
var c_radius:Number = new Number();
var _ypos:Number = new Number();
var ground:Number = new Number();
var howlong:Number = new Number();
var howlongdrop:Number = new Number();
ground = stage.stageHeight;
//trace(stage.stageHeight);
//var i:Number = new Number();
addEventListener(Event.ENTER_FRAME, spawn);
function spawn(n:Event)
{
howlongdrop = Math.ceil(Math.random() * 300) / 100;
howlong = Math.ceil(Math.random() * stage.stageWidth);
_ypos = Math.ceil(Math.random() * stage.stageHeight);
c_radius = Math.ceil(Math.random() * 20);
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0x000000, 2);
sprite.graphics.drawCircle(0,0,c_radius);
sprite.graphics.endFill();
sprite.y = _ypos;
sprite.alpha = Math.ceil(Math.random() * 100) / 100;
addChild(sprite);
//var where:Number = stage.stageHeight-_ypos;
behave(sprite, ground, howlong, howlongdrop);
}
function behave(i:Sprite, groundy:Number, howlongfly:Number, howlongdrop2:Number)
{
var _fly:Tween = new Tween(i,"x",Strong.easeOut,i.x,i.x + howlongfly,2,true);
var drop:Tween = new Tween(i,"y",Bounce.easeOut,i.y,groundy,howlongdrop2,true);
_fly.addEventListener(TweenEvent.MOTION_FINISH, dissapear);
}
function dissapear(n:Event) {
//here I'm somehow supposed to get i:Sprite from function behave and get it's alpha 0 then remove child. How do I get to this i:Sprite if in event handlers there is no room for vars when calling a function?
}
|
|
|
08-06-2012, 01:46 PM
|
#7
|
|
Registered User
Join Date: Aug 2012
Posts: 6
|
I fixed that! Hurray! But now - all my particles are just freezing. Why, I cannot figure
ActionScript Code:
import fl.transitions.*;
import fl.transitions.easing.*;
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Sprite;
var c_radius:Number = new Number();
var _ypos:Number = new Number();
var ground:Number = new Number();
var howlong:Number = new Number();
var howlongdrop:Number = new Number();
var maxnumber:Number = new Number(100);
var totalnumber:Number = new Number(0);
ground = stage.stageHeight;
addEventListener(Event.ENTER_FRAME, spawn);
function spawn(n:Event)
{
howlongdrop = Math.ceil(Math.random() * 300) / 100;
howlong = Math.ceil(Math.random() * stage.stageWidth);
_ypos = Math.ceil(Math.random() * stage.stageHeight);
c_radius = Math.ceil(Math.random() * 20);
if (totalnumber < maxnumber)
{
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0x000000, 2);
sprite.graphics.drawCircle(0,0,c_radius);
sprite.graphics.endFill();
sprite.y = _ypos;
sprite.alpha = Math.ceil(Math.random() * 100) / 100;
addChild(sprite);
totalnumber++;
behave(sprite, ground, howlong, howlongdrop);
}
else
{
}
}
function behave(i:Sprite, groundy:Number, howlongfly:Number, howlongdrop2:Number)
{
var _fly:Tween = new Tween(i,"x",Strong.easeOut,i.x,i.x + howlongfly,2,true);
var drop:Tween = new Tween(i,"y",Bounce.easeOut,i.y,groundy,howlongdrop2,true);
_fly.addEventListener(TweenEvent.MOTION_FINISH, dissapear);
}
function dissapear(n:TweenEvent)
{
var __z:Object;
__z = n.currentTarget;
totalnumber--;
removeChild(__z.obj);
}
|
|
|
08-06-2012, 04:35 PM
|
#8
|
|
Registered User
Join Date: Aug 2012
Posts: 6
|
Quote:
Originally Posted by [afz]snickelfitz
If you like the Tween class, you'll love Greensock TweenMax and TimelineMax.
Seriously, these classes are awesome for building particle systems and complex sequences. (and no GC issues like the Adobe Tween class)
|
Oh my god, after one day I understood what you was saying. Thank you. GC is my problem and I'm a dumbass. I'll use this new cool TweenMax class
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 06:50 PM.
///
|
|