A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Problem with Event Listener ENTER_FRAME

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    10

    Problem with Event Listener ENTER_FRAME

    Hello everyone,

    I have a problem with Events. I have a lot of them because I'm making a shooting game. I never noticed it before adding sounds, but a lot of commands I've made are stacked over +/- 15 times.

    How can I calm a "stage.addEventListener(Event.ENTER_FRAME, functionname)" and make my MouseEvent.CLICK (or MOUSE_DOWN) working once instead of spamming my box and stacking music ?

    Actionscript Code:
    //Variables

    var SiloRotRad:Number;
    var SiloRotDeg:Number;
    var MissileSimpleS:MissileSimpleSound = new MissileSimpleSound();

    stage.addEventListener(Event.ENTER_FRAME, onEnterGame);
    function onEnterGame(event)
    {
        if (currentFrame == 6)
        {
       
       
        //Cacher la souris et la remplacer
        Mouse.hide();
        stage.addEventListener(MouseEvent.MOUSE_MOVE,suivresouris);
        function suivresouris(evt:MouseEvent)
        {
        Curseur.x = mouseX;
        Curseur.y = mouseY;
        }
       
        //Rotation du curseur


        //Tir
        stage.addEventListener(Event.ENTER_FRAME, frameEntered);
        stage.addEventListener(MouseEvent.MOUSE_DOWN, Tir);

        function frameEntered(event:Event):void
        {
        SiloRotRad = Math.atan2(mouseY - Silo.y, mouseX - Silo.x);
        SiloRotDeg = SiloRotRad * 180 / Math.PI;
        Silo.rotation = SiloRotDeg;
        }
       

       

        function Tir(event:MouseEvent):void
        {  
        var newBullet:MovieClip = new MissileSimple();
        newBullet.x = Math.cos(SiloRotRad) * 10 + Silo.x;
        newBullet.y = Math.sin(SiloRotRad) * 10 + Silo.y;
        newBullet.rotation = Silo.rotation;
        addChild(newBullet);
        MissileSimpleS.play();
        trace(addChild(newBullet));
        }

       
       
       
       
       
        } // If Ingame 
    } // OnEnterGame

    For example, here, in my actual shooting room, when I click once, missile sound and missile entity are stacked over 15 times each.


    Thanks

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Do not nest functions. Since you define and add Tir within onEnterGame, every time onEnterGame executes, you get a new function Tir as a MOUSE_DOWN listener.

    There is no need for onEnterGame at all.

    Also, although not technically errors, your code style and naming conventions are very non-standard and make things harder to read. Variables and functions should start with lowercase letters. Only classes should start with uppercase. Code should be indented further in each nested block. So this:
    Code:
        function frameEntered(event:Event):void{
          siloRotRad = Math.atan2(mouseY - silo.y, mouseX - silo.x);
          siloRotDeg = siloRotRad * 180 / Math.PI;
          silo.rotation = siloRotDeg;
        }
    Not this:
    Code:
        function frameEntered(event:Event):void
        {
        SiloRotRad = Math.atan2(mouseY - Silo.y, mouseX - Silo.x);
        SiloRotDeg = SiloRotRad * 180 / Math.PI;
        Silo.rotation = SiloRotDeg;
        }

  3. #3
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    760
    Look here on nested functions.

    Pull out the entire Tir function and looks like will work fine.
    [SIGPIC][/SIGPIC]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center