making a symbol move by keyboard not showing result and when published it not reads stop(); but replays it again and again

StackOverflow https://stackoverflow.com/questions/19178951

  •  30-06-2022
  •  | 
  •  

Question

I am new to actionscript ,

My document class is ,

package
{
    //list of our imports these are classes we need in order to
    //run our application.
    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.events.Event;

    public class engine extends MovieClip
    {

        // moved ourShip to a class variable.
        private var Circle:circle = new circle()
        //our constructor function. This runs when an object of
        //the class is created
        public function engine()
        {
            addFrameScript(0, frame1);
            addFrameScript(1, frame2);
        }
        // frame 1 layer 1 --------------------------------------------------
        public function frame1()
        {
        stop();
        }
        //-------------------------------------------------------------------
        // frame 2 layer 1 --------------------------------------------------
        public function frame2()
        {
        Circle.x = stage.stageWidth / 2;
        Circle.y = stage.stageHeight / 2;
        addChild(Circle);
        }
        //-------------------------------------------------------------------
    }
}

i made two frames first contains button and the other circle which i want to move but it not moves and it stays in the middle on second frame

My button class is

package
{
//imports
import flash.events.MouseEvent;
import flash.display.SimpleButton;
import flash.display.MovieClip;
//-------
public class start extends SimpleButton
 {
   public function start()
   {
   addEventListener(MouseEvent.CLICK, onTopClick);
   addEventListener(MouseEvent.MOUSE_OVER, onBottomOver);
   }

   function onTopClick(e:MouseEvent):void
   {
     MovieClip(root).gotoAndStop(2)
   }

   function onBottomOver(e:MouseEvent):void
   {
       }
 }
}

And my as of circle movieclip is

package
{
    //imports
    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.ui.Keyboard;
    import flash.events.Event;
    import flash.events.KeyboardEvent; 

  public class circle extends MovieClip
  {
    private var speed:Number = 0.5;
    private var vx:Number = 0;
    private var vy:Number = 0;
    private var friction:Number = 0.93;
    private var maxspeed:Number = 8;
    public function circle()
    {
        addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
    }
    public function loop(e:Event) : void
    {
        addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
        x+=vx;
        y+=vy
    }
    function keyHit(event:KeyboardEvent):void {
        switch (event.keyCode) {
            case Keyboard.RIGHT :
            vx+=speed;
            break;
            case Keyboard.LEFT :
            vx-=speed;
            break;
            case Keyboard.UP :
            vy-=speed;
            break;
            case Keyboard.DOWN :
            vy+=speed;
            break;
            }

            }
  }
}

I am sorry to post so much for you guys to read but stackoverflow is the only website where anyone helps me !

Was it helpful?

Solution

You have made several major errors. First, addFrameScript() isn't a proper way to place code on frames, use Flash's editor to place code on timeline. (IIRC you will have to make a single call out of your two in order to have all the code you add to function) And, whatever code you added to a frame of a MC is executed each frame if the MC's currentFrame is the frame with code. Thus, you are adding a function "frame2()" that places the Circle in the center of the stage each frame! You should instead place it at design time (link it to a property) into the second frame, or in a constructor, or you can use one single frame and Sprite instead of MovieClip, and instead of using frames you can use container sprites, adding and removing them at will, or at an action.

The other major mistake is adding an event listener inside an enterframe listener - these accumulate, not overwrite each other, so you can have multiple functions be designated as listeners for a particular event, or even one function several times. The latter happens for you, so each frame another instance of a listening keyHit function is added as a listener. The proper way to assign listeners is either in constructor, or in any function that listens for manually triggered event (say, MouseEvent.CLICK), but then you have to take precautions about listening for more than once with each function, and listening only with those functions you need right now.

EDIT: Okay. Your code was:

        addFrameScript(0, frame1);
        addFrameScript(1, frame2);

The more correct way should be:

addFrameScript(0,frame1,1,frame2);

The reason is, the call to addFrameScript replaces all the timeline code with what you supply within here. The function is undocumented, perhaps by the reason of its affects on the stage and AS3 environment. The closest thing to the documentation on addFrameScript() so far is this link.

Next: Your code is:

public function circle()
{
    addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
    addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
    x+=vx;
    y+=vy
}

The correct way of writing this is as follows:

public function circle()
{
    addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
    if (stage) init();
    else addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event=null):void 
{
    removeEventListener(Event.ADDED_TO_STAGE,init);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit);
}
public function loop(e:Event) : void
{
    x+=vx;
    y+=vy
}

The listeners should be assigned in constructor, if they are permanent or you want them to be active as soon as you create an object. The KeyboardEvent listeners are separate case, as in order for them to function you have to assign them to stage, which is not available right at the time of creating the object, so you need an intermediate layer - the init() function, that is only called when the object is added to stage. At this point stage is no longer null, and you can assign an event listener there. Note, if you want to make your circles eventually disappear, you have to remove the listener you assigned to stage at some point of your removal handling code.

Next: Your code:

    public function frame2()
    {
    Circle.x = stage.stageWidth / 2;
    Circle.y = stage.stageHeight / 2;
    addChild(Circle);
    }

Correct code should be:

public function frame2():void 
{
    if (Circle.parent) return; // we have added Circle to stage already!
    Circle.x = stage.stageWidth / 2;
    Circle.y = stage.stageHeight / 2;
    addChild(Circle);
}

See, you are calling this every time your MC is stopped at second frame, thus you constantly reset Circle's coordinates to stage center, so you just cannot see if it moves (it doesn't, as you have assigned the keyboard listener not to stage).

Perhaps there are more mistakes, but fixing these will make your MC tick a little bit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top