Question

I have created a program in Actionscript 3 (Flash CS5) that spawns an Egg (a circle for now) off the stage when you click, and then sends the Egg to where you clicked with a (coded) tween. Additionally, it scales it in attempt to give an illusion of 3D.

I add a TweenEvent.MOTION_FINISH to one of the tweens for the Egg, to check when the motion finishes. When it does, I create a new object (called Splat) with the same coordinates as the original mouse click.

Problem 1: The Splat object isn't added in the correct spot, as seen in the picture below. I check the coordinates, and they match in both the Egg class and the Splash class. I have also made sure that the registration point for both objects is in the middle of them. Finally, I have checked to see if it was the scale that made the problem, but I get the same problem even if I remove the scale tweens.

(EDIT: The Egg is placed in the right spot. The original coordinates for the mouse click are as they should be. It is ONLY the star objects that seem to be displaced.)

http://i45.tinypic.com/24bj1h3.png

Problem 2: The Splat object is scaled down the same amount as the Egg object. Why is this? I haven't (intended to) make the Splash dependent on the Egg.

And here is the relevant code:

package  
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.MouseEvent;

public class eggMain extends MovieClip
{
    import flash.events.Event;

    public function eggMain() 
    {
        stage.addEventListener(MouseEvent.CLICK, spawn);
    }

    function spawn(event)
    {
        for(var i:int = 0; i<1; i++)
        {
            trace("mX in main class = " + mouseX);
            trace("mY in main class = " + mouseY);
            var e:Egg = new Egg(mouseX, mouseY);
            addChild(e);

        }
    }
}
}


package  
{
import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;


public class Egg extends MovieClip 
{
    var stageWidth:int = 550;
    var stageHeight:int = 400;
    var buffer:int = 100;
    var mX = 0;
    var mY = 0;

    public function Egg(moX:int, moY:int) //mouseX, mouseY
    {
        mX = moX;
        mY = moY;
        trace("mX in Egg constructor: " + mX);
        trace("mY in Egg constructor: " + mY);

        spawnAtFixedPoint();
        animate();
    }




    function spawnAtFixedPoint()
    {
        this.x = stageWidth + buffer;
        this.y = stageHeight - buffer;
    }

    //Moves the egg in from the spawn point using tweens. Scales down on the way to give 3D illusion.
    function animate()
    {
        var xMovement:Tween;
        var yMovement:Tween;
        var xScale:Tween;
        var yScale:Tween;

        trace("mX in animate() = " + mX);
        trace("mY in animate() = " + mY);
        xMovement = new Tween(this, "x", Regular.easeOut, this.x, mX, 1, true); //obj, property, ease function, start, time, in seconds (true)
        yMovement = new Tween(this, "y", Regular.easeOut, this.y, mY, 1, true);
        xScale = new Tween(this, "scaleX", Regular.easeIn, 1, 0.1, 1, true);
        yScale = new Tween(this, "scaleY", Regular.easeIn, 1, 0.1, 1, true);


        //Checks when the object has finished animating (which mean the egg has landed)
        xMovement.addEventListener(TweenEvent.MOTION_FINISH, splat);
    }

    //Spawns a splat (currently a star) at the same coordinates as the egg
    function splat(event)
    {
        trace("mX in splat() = " + mX);
        trace("mY in splat() = " + mY);
        var s = new Splat(mX, mY);
        addChild(s);
    }

}
}

Update: I created a custom SplatEvent and had my main class listen for it. The even is dispatched in the MOTION_FINISHED handler. It works!

Although, I had to make it work by using public static vars in the main class for passing back the original coordinates of the Egg landing. Like so in the main class:

public static var splatX:int;
public static var splatY:int;

And like this in the Egg class:

eggMain.splatX = mX;
eggMain.splatY = mY;

I can live with it, but I would prefer not to use global variables. Taking suggestions for this, although the main question is answered. Thanks.

Était-ce utile?

La solution

You're creating the splat within the egg instance and then adding it to the egg's display list. This means that the splat (star) is created in the egg's coordinate space and is therefore offset by the egg's position on the stage. It also means that the splat inherits any transformations you've made to the egg, such as the scale, as it is a child of the transformed egg instance.

A better approach would be to listen in your main class for either your egg's motion finish event (have a look at event bubbling) or a custom event fired from the motion finish handler. When that event is received, create an instance of splat and add it to the display list of the main class.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top