A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: Problem with spawning engine

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    5

    Problem with spawning engine

    For problem solvers!
    Hello, this is a problem involving spawning objects.
    The code below creates this.
    http://www.swfcabin.com/open/1304522916
    To "play" simply left click once

    Before spawning an atom the current code makes sure that an object wont collide with any existing objects, just as I want it to be. However once a subatom has been spawned the coming atoms wont spawn at any of the three remaining binding points "left bottom top or right". (as it should)

    The only error message appearing (occasionally): a term is undefined and has no properties at atom_fla::MainTimeline/doAll()


    Code:
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.display.MovieClip;
    var PhotonActive:Boolean = false;
    var slumpSpawn:Number=20;
    var score:Number=100;
    var mainAtom:bigAtom = new bigAtom(); 
    mainAtom.x=365;
    mainAtom.y=350;
    mainAtom.bind1.boundToAtom="";
    mainAtom.bind1.boundToBind="";
    mainAtom.bind1.dir="Left"; 
    mainAtom.bind2.boundToAtom="";
    mainAtom.bind2.boundToBind="";
    mainAtom.bind2.dir="Right";
    mainAtom.bind3.boundToAtom="";
    mainAtom.bind3.boundToBind="";
    mainAtom.bind3.dir="Top";
    mainAtom.bind4.boundToAtom="";
    mainAtom.bind4.boundToBind="";
    mainAtom.bind4.dir="Bottom";
    stage.addChild(mainAtom);  
    var spawnLista:Array = new Array(); 
    var openSpot:Array = new Array(); 
    openSpot.push(new Array(mainAtom,mainAtom.bind1)); 
    openSpot.push(new Array(mainAtom,mainAtom.bind2));
    openSpot.push(new Array(mainAtom,mainAtom.bind3));
    openSpot.push(new Array(mainAtom,mainAtom.bind4));
    
    stage.addEventListener(MouseEvent.MOUSE_DOWN,mousePress);
    stage.addEventListener(Event.ENTER_FRAME,doAll);
    
    
    function doAll(evt:Event):void{
    		 
    		 
    // Move Photon
    if(PhotonActive){
    		var distx:Number = mouseX-Photon.x;
    		var disty:Number = mouseY-Photon.y;
    var distance:Number = Math.sqrt(distx*distx + disty*disty);
            var angle:Number = Math.atan2(disty, distx);
            var Move:Number = distance;
    if (Move > 5)
      Move = 5;
    }
    
    if (distance > 2){
      Photon.x += Math.cos(angle)*Move;
      Photon.y += Math.sin(angle)*Move;
    }
    /*
    
    	if(PhotonActive){
    	
    		if(Math.abs(distx)>2)
    			Photon.x+=5*distx/Math.abs(distx);
    		if(Math.abs(disty)>2)
    			Photon.y+=5*disty/Math.abs(disty);
    	}
    	*/
    // Handle Atoms
    slumpSpawn--;
    	if(slumpSpawn<1){
    		var reDo:Boolean= true;
    		while(reDo){
    			reDo=false;
    			if(openSpot.length>0){
    				var rand:Number = Math.floor(Math.random()*openSpot.length);
    				var spot:Array = openSpot.splice(rand,1);
    				var bindName:String=spot[0][1].name;
    				if(spot[0][1].boundToBind==""){
    				
    					var spawnAtom:atomm = new atomm(); 
    					// Initiate spawnAtom
    					
    					spawnAtom.scaleX=.3;
    					spawnAtom.scaleY=.3;
    					spawnAtom.name="atom"+(spawnLista.length+1);
    					spawnAtom.bind1.boundToBind="";
    					spawnAtom.bind1.boundToAtom="";
    					spawnAtom.bind1.dir="Left";
    					spawnAtom.bind2.boundToBind="";
    					spawnAtom.bind2.boundToAtom="";
    					spawnAtom.bind2.dir="Right";
    					spawnAtom.bind3.boundToBind="";
    					spawnAtom.bind3.boundToAtom="";
    					spawnAtom.bind3.dir="Top";
    					spawnAtom.bind4.boundToBind="";
    					spawnAtom.bind4.boundToAtom="";
    					spawnAtom.bind4.dir="Bottom";
    					spawnAtom.count=4;  
    					spawnAtom.maxCount=4;
    					var bind:String="";
    					if(spot[0][1].dir=="Left")  // Have to connect to right
    						bind="bind2";
    					if(spot[0][1].dir=="Right")  // Have to connect to Left
    						bind="bind1";
    					if(spot[0][1].dir=="Top")  // Have to connect to Bottom
    						bind="bind4";
    					if(spot[0][1].dir=="Bottom")  // Have to connect to Top
    						bind="bind3";
    					spawnAtom[bind].boundToBind=bindName;  
    					spawnAtom[bind].boundToAtom=spot[0][0];  
    					spot[0][1].boundToBind=bind; 
    					spot[0][1].boundToAtom=spawnAtom; 
    					spawnAtom.x=(spot[0][0].x+spot[0][0].scaleX*spot[0][1].x)-(spawnAtom.scaleX*spawnAtom[bind].x);
    					spawnAtom.y=(spot[0][0].y+spot[0][0].scaleY*spot[0][1].y)-(spawnAtom.scaleY*spawnAtom[bind].y);
    				
    				
    					var checkTal:Number=0;
    					while(checkTal<spawnLista.length){
    						if(spawnLista[checkTal].name!=spot[0][0].name){ 
    							if(spawnAtom.hitTestObject(spawnLista[checkTal])){
    								trace("asdasd");
    							  reDo=true;
    							}
    						}
    						checkTal++;
    					}
    					if(!reDo){
    						if(bind!="bind1"){ openSpot.push(new Array(spawnAtom,spawnAtom.bind1));}
    						if(bind!="bind2"){ openSpot.push(new Array(spawnAtom,spawnAtom.bind2));}
    						if(bind!="bind3"){ openSpot.push(new Array(spawnAtom,spawnAtom.bind3));}
    						if(bind!="bind4"){ openSpot.push(new Array(spawnAtom,spawnAtom.bind4));}
    						spawnLista.push(spawnAtom);
    						stage.addChild(spawnAtom);
    					}
    					if(reDo){
    						spot[0][1].boundToBind=""; 
    						spot[0][1].boundToAtom=""; 
    						openSpot.push(spot);
    					
    					
    				}
    			}
    		}
    		slumpSpawn=Math.random()*50+30;
    	}
    // Handle Atoms
    	var binds:Number=0;
    	var j:Number;
    	for(var  spNo:Number = 0; spNo <spawnLista.length;spNo++){
    //		trace(i);
    		for( var elNo:Number =1; elNo<=spawnLista[spNo].maxCount;elNo++){
    					//trace(j);
    
    			var namn:String = "e"+elNo;
    			if(spawnLista[spNo][namn].visible){
    				if(spawnLista[spNo][namn].hitTestObject(Photon)){
    
    
    //
    					binds=0;
    					var lastBind:Number=0;
    					for(j=1;j<5;j++){
    						if(spawnLista[spNo]["bind"+j].boundToBind!=""){
    							//trace(spawnLista[spNo]["bind"+j].boundToBind);
    							binds++;
    							lastBind=j;
    						}
    					}
    //					trace(binds);
    					if(binds==1){
    						spawnLista[spNo].count--;
    						spawnLista[spNo][namn].visible=false;
    
    						if(spawnLista[spNo].count==0){
    							for(j=0;j<openSpot.length;j++){
    								if(openSpot[j][0].name==spawnLista[spNo].name){
    									openSpot.splice(j,1);
    									j--;
    								}
    							}
    							var tempKoppling:MovieClip=spawnLista[spNo]["bind"+lastBind].boundToAtom;
    							var tempbind:String=spawnLista[spNo]["bind"+lastBind].boundToBind;
    							tempKoppling[tempbind].boundToAtom="";
    							tempKoppling[tempbind].boundToBind="";
    							openSpot.push(new Array(tempKoppling,tempKoppling[tempbind]));
    							stage.removeChild(spawnLista[spNo]);
    						}
    					}
    				}
    			}
    		}
    	}
    This problem is caused by the code checking the collisions of coming objects.

    Removing this code makes this:
    http://www.swfcabin.com/open/1304522783

    if you wait for a bit you'll see that the objects spawn onto each other.
    Code:
    while(checkTal<spawnLista.length){
    						if(spawnLista[checkTal].name!=spot[0][0].name){ 
    							if(spawnAtom.hitTestObject(spawnLista[checkTal])){
    							
    							  reDo=true;
    							}
    						}
    						checkTal++;
    					}
    					if(!reDo){
    Bottomline, I need this code to make the game playable, suggestions of different solutions or other changes to simply make it better are highly appreciated!
    Last edited by flytex; 05-04-2011 at 12:26 PM.

Tags for this Thread

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