Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have made several major errors. First, <code>addFrameScript()</code> 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 <code>currentFrame</code> is the frame with code. Thus, you are adding a function "frame2()" that places the <code>Circle</code> 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 <code>Sprite</code> instead of <code>MovieClip</code>, and instead of using frames you can use container sprites, adding and removing them at will, or at an action.</p> <p>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 <code>keyHit</code> 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, <code>MouseEvent.CLICK</code>), 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.</p> <p>EDIT: Okay. Your code was:</p> <pre><code> addFrameScript(0, frame1); addFrameScript(1, frame2); </code></pre> <p>The more correct way should be:</p> <pre><code>addFrameScript(0,frame1,1,frame2); </code></pre> <p>The reason is, the call to <code>addFrameScript</code> <strong>replaces</strong> 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 <code>addFrameScript()</code> so far is <a href="http://www.as3dp.com/2009/09/a-closer-look-using-movieclipaddframescript/" rel="nofollow">this link</a>.</p> <p>Next: Your code is:</p> <pre><code>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 } </code></pre> <p>The correct way of writing this is as follows:</p> <pre><code>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 } </code></pre> <p>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 <code>KeyboardEvent</code> listeners are separate case, as in order for them to function you have to assign them to <code>stage</code>, which is not available right at the time of creating the object, so you need an intermediate layer - the <code>init()</code> function, that is only called when the object is added to stage. At this point <code>stage</code> 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.</p> <p>Next: Your code:</p> <pre><code> public function frame2() { Circle.x = stage.stageWidth / 2; Circle.y = stage.stageHeight / 2; addChild(Circle); } </code></pre> <p>Correct code should be:</p> <pre><code>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); } </code></pre> <p>See, you are calling this every time your MC is stopped at second frame, thus you constantly reset <code>Circle</code>'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).</p> <p>Perhaps there are more mistakes, but fixing these will make your MC tick a little bit.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload