Skip to main content
October 4, 2010
Question

quirky loop AS3

  • October 4, 2010
  • 1 reply
  • 502 views

Well I have a very weird quirk with AS3.
I load 4 images and move it with a mouse.(commented out mouse movements)
But this program contnually loads images and after commenting out every movemnt it still loads not 4 images but the same 4 images over and over again. I get a continual set of images displayed in the same spot.

It will work if I start a new project but I cant keep starting new projects when I get a bug?
I deleted the swf but this has no effect.

// fla file
var noLoaded:int;
noLoaded=0;

var li2:player= new player();
li2.addEventListener("image_loaded",imageLoadedHandler);
addChild(li2);



function imageLoadedHandler(e:Event){
     stage.addEventListener(KeyboardEvent.KEY_DOWN,doSomething);
  noLoaded+=1;
}


 function doSomething(e:KeyboardEvent):void
     {
          
          if (e.keyCode==37) //39 is right arrow
          {
          
          ///trace("left pressed")
          //li2.moveleft();
          
          }
          
          if (e.keyCode==39) //39 is right arrow
          {
          
          ///trace("right pressed")
          ///li2.moveright();
          
          //trace("right pressed")
          //li2.movedown();
          
          }
          
          if (e.keyCode==38) //39 is right arrow
          {
          
          //trace("right pressed")
          //li2.moveup();
          
          }
          
     }
     
stage.addEventListener(Event.ENTER_FRAME, gameLoop);




function gameLoop(event:Event)
      {
     if (noLoaded>=1)
          {
               //li3.moveChar();
          //trace("all loaded");     
          }
     }
     

////  player.as


package  {
      import flash.display.*;
          import flash.display.Bitmap;
          import flash.display.Loader;
          import flash.display.Sprite;
          import flash.events.Event;
          import flash.net.URLRequest;
          import flash.text.*;
          import flash.events.MouseEvent;
          import flash.events.KeyboardEvent;
          import flash.ui.Keyboard;
     public class player  extends Sprite{

          private var imgStr:String;
          private var myx:int;
          private var myy:int;
          private var img2:String;
          private var myx2:int;
          private var myy2:int;
          private  var sp:Sprite=new Sprite();
          private   var sp2:Sprite=new Sprite();
          private   var img1:Bitmap;
          private   var bmp:BitmapData;
            private var MyImages:Array = [];
             private var MyBData:Array = [];
            private var xx:int;
             private var i:int;
             private var timej:int;
             
            var urls:Array = ["scave4.png", "scave5.png", "scave6.png", "scave7.png"];

            
          public function player()     {
               
                    xx=10;
               i=0;
               
                 timej=0;
                 
               for each (var el:String in urls) {
                    var loader:Loader = new Loader;
                    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
                    loader.load(new URLRequest(el));
                   
                   // trace(el + "dsds");
                    }

          
          
          }
          
          private function imageLoaded(event:Event):void
          {
                
               var Singleimage:Bitmap = new Bitmap(event.target.content.bitmapData);
                      //var bmd:BitmapData = event.target.loader.content.bitmapData;

                    var  bmd=Singleimage.bitmapData;
                    //img1=new Bitmap(bmd);
                         MyBData.push(bmd);
          
            MyImages.push(Singleimage);
          
          //img1.bitmapData = bmd;
          
          //addChild(MyImages[0]);
          //addChild(MyImages);
          
          //MyImages.visible=false;
          
          
          if (i==0)
          {
               img1=new Bitmap(bmd);
               img1.bitmapData = MyBData[0];
               addChild(img1);
               img1.y=100;
               img1.x=100;
               img1.visible=true;
          }
          i+=1;
          
          
          //img1.x =xx;
          //img1.y=100;
          
          //xx+=40;
            if (i>=4)
          {
               i=0;
          }
            
             dispatchEvent(new Event("image_loaded"));

          }
          
          



          public function moveleft()
          {
               
               animateImage()
               img1.x-=5;
               
          }
          
          public function moveright()
          {
               
               animateImage()
                    
               img1.x+=5;
               
          }
          
          public function moveup()
          {
               
               animateImage()
                    
               img1.y-=5;
               
          }
          
          public function movedown()
          {
               
               animateImage()
                    
               img1.y+=5;
               
          }
          
          
          private function animateImage()
          {
                var xpos:int;
                    
                    
                    timej+=1;
                    if (timej>=10)
                    {
                         
                              i+=1;
                              
                              
                              if (i>=4)
                              {
                                   i=0
                              }
                         img1.bitmapData=MyBData;
                    
                    }
               
               
               
          }

     }
     
}
This topic has been closed for replies.

1 reply

Inspiring
October 4, 2010

This is because every time frame 1 is hit on the main timeline code is executed again, Try:

var li2:player;
if(!li2) {
     li2 = new player();
     li2.addEventListener("image_loaded",imageLoadedHandler);
     addChild(li2);
}

October 4, 2010

ok it works and could you explain again why as I am new to AS3

So all my code loop around since it is in the timeline since the code will work if I start a new project so it doesnt always loop.

Now what I want is the code not to loop around and I start the loop  with the game loop so how do I do this?