A Flash Developer Resource Site

Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27

Thread: Instance creation problem

  1. #21
    Junior Member
    Join Date
    Feb 2012
    Posts
    17
    army is declared in the class
    Code:
    public var army:Array;
    When I try to declare the newEnemy, the code doesn't work, here is what I done:
    Code:
    			for (var i:int = army.length-1; i>=0; i--)
    			{	
    				var newEnemy=new Enemy(x,y);
    			  	var nme:Enemy = army[i];
     			 	if (newEnemy.hitTestObject(nme))
    			 	{
     			  		 nme.y+=120;
    				}
    			}
    P.S. I don't know how to declare newEnemy in such a way so it doesn't interfere with other code and i also get a warning:
    Code:
    Warning: 3596: Duplicate variable definition.
    Last edited by szymek.k3; 03-07-2012 at 05:19 PM.

  2. #22
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If you are getting duplicate variable definition, that means that you've already declared that variable in that scope.

    You wouldn't want to create a new Enemy each time through the loop anyway, you'd end up creating as many new enemies as you already had enemies, which is going to get big very fast.

    Since you're trying to determine whether you want to create a new enemy in the first place, you should not have one until you decide that you want one.

    What you should do is iterate through all your existing enemies (which you're doing), but what you should do for each enemy is determine if it hits the proposed point, not another enemy. You could check that it's within some distance of a point too, if you want some margin.
    In this case, because you are not altering the army array during the iteration, you do not need to start from the end and go backward. Any old iteration order will do, even the default.

    x and y are not great variable names for the proposed spot, since they will shadow the x and y properties of the instance containing this code. I'll change them to px, py for this.

    Code:
    var validSpot:Boolean = true;
    for (var i:int = 0; i < army.length; i++){
      var nme:Enemy = army[i];
      if (nme.hitTestPoint(px,py,true)){
        validSpot = false;
        break;
      }
    }
    
    if (validSpot){
      //insert code to spawn a new Enemy at px,py.
    }

  3. #23
    Junior Member
    Join Date
    Feb 2012
    Posts
    17
    Now enemies are created in a straight line one after the other I had to declare px was x and py was y
    Code:
    			var px:Number=x+500;
    			var py:Number=y;
    			var validSpot:Boolean = true;
    			for (var i:int = 0; i < army.length; i++){
      			var nme:Enemy = army[i];
      			if (nme.hitTestPoint(px,py,true)){
        		validSpot = false;
        		break;
      			}
    			}
    
    			if (validSpot)
    			{
     				var newEnemy = new Enemy(px,py);
    				army.push(newEnemy);
    				addChild(newEnemy); //insert code to spawn a new Enemy at px,py.
    			}

  4. #24
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Why are you passing x and y values to the Enemy constructor? If all it's doing is setting its own x and y coordinates, then that's okay but kind of silly. You don't pass alpha, scaleX, scaleY, etc. I'd just set the coordinates after creating.

    Anyway, where the enemies get placed will be the point at px, py. It's up to you to select that point appropriately.

  5. #25
    Junior Member
    Join Date
    Feb 2012
    Posts
    17
    Did you mean something like this?
    Code:
    var px:Number =600;
    			var py:Number = Math.random()*400;
    			var validSpot:Boolean = true;
    			for (var i:int = 0; i < army.length; i++){
      			var nme:Enemy = army[i];
      			if (nme.hitTestPoint(px,py,true)){
        		validSpot = false;
        		break;
      			}
    			}
    
    			if (validSpot)
    			{
     				var newEnemy = new Enemy(x,y);
    				army.push(newEnemy);
    				addChild(newEnemy); //insert code to spawn a new Enemy at px,py.
    				enemy.x=px;
    				enemy.y=py;
    			}
    It overlaps any way.
    I think it would be easier if you seen my game so you could see everything
    "Moja Gra" means "My Game"

  6. #26
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Can't look at that at work, filters.

    hitTestPoint expects the coordinates passed in to be in the Stage coordinate space. If your Enemies are in some container offset from the Stage origin, then it will be testing the wrong point. You can use localToGlobal to get global coordinates from local coordinates.

  7. #27
    Junior Member
    Join Date
    Feb 2012
    Posts
    17
    Ehh this is quite confusin because the solutions look logican but don't work.
    Here is my Game class code where everything is controlled:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	import flash.utils.Timer;
    	import flash.events.TimerEvent;
    	import flash.events.KeyboardEvent;
    	import flash.ui.Keyboard;
    	
    	public class MojaGra extends MovieClip
    	{
    		//Variables
    		public var player:Player;
    		public var gametimer:Timer;
    		public var Downkey:Boolean;
    		public var Upkey:Boolean;
    		public var Leftkey:Boolean;
    		public var Rightkey:Boolean;
    		public var Myspeed;
    		public var army:Array;
    		public var spawn:Spawn;
    		public var speed;
    		
    		public function MojaGra()
    		{
    			//Speed
    			Myspeed=4;
    			speed=3;
    			
    			//Objects
    			player= new Player();
    			addChild(player);
    			player.x = 200;
    			player.y = 200;
    			
    			army = new Array();
    			var randomX:Number = Math.random()*400;
    			var newEnemy = new Enemy( 100, 100);
    			army.push(newEnemy);
    			addChild(newEnemy);
    
    			//Timer
    			gametimer = new Timer(24);
    			gametimer.addEventListener( TimerEvent.TIMER, Tick);
    			gametimer.start();
    			
    			//Keyboard Listerners
    			stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyPress);
    			stage.addEventListener( KeyboardEvent.KEY_UP, onKeyRelease);
    		}
    		
    		public function Tick( timerEvent:TimerEvent):void
    		{
    			speed+=0.001
    			//Enemy Movement
    			for each (var enemy:Enemy in army)
    			{
    				enemy.Move(speed);
    			}
    
    			//Enemy Creation and Placement
    			var px:Number =600;
    			var py:Number = Math.random()*400;
    			var validSpot:Boolean = true;
    			for (var i:int = 0; i < army.length; i++){
      			var nme:Enemy = army[i];
      			if (nme.hitTestPoint(px,py,true)){
        		validSpot = false;
        		break;
      			}
    			}
    
    			if (validSpot)
    			{
     				var newEnemy = new Enemy(x,y);
    				army.push(newEnemy);
    				addChild(newEnemy); //insert code to spawn a new Enemy at px,py.
    				enemy.x=px;
    				enemy.y=py;
    			}
    
    			//Keyboard Handling
    			if (Downkey==true)
    			{
    				player.y+=Myspeed;
    			}
    			
    			if (Upkey==true)
    			{
    				player.y-=Myspeed;
    			}
    			
    			if (Rightkey==true)
    			{
    				player.x+=Myspeed;
    			}
    			
    			if (Leftkey==true)
    			{
    				player.x-=Myspeed;
    			}
    		}
    		
    		//Keyboard Handling
    		public function onKeyPress(keyboardEvent:KeyboardEvent):void
    		{
    			if( keyboardEvent.keyCode == Keyboard.DOWN )
    			{
    				Downkey=true;
    			}
    			
    			if( keyboardEvent.keyCode == Keyboard.UP )
    			{
    				Upkey=true;
    			}
    			
    			if( keyboardEvent.keyCode == Keyboard.LEFT )
    			{
    				Leftkey=true;
    			}
    			
    			if( keyboardEvent.keyCode == Keyboard.RIGHT )
    			{
    				Rightkey=true;
    			}
    		}
    		
    		//Keyboard Handling
    		public function onKeyRelease(keyboardEvent:KeyboardEvent):void
    		{
    			if( keyboardEvent.keyCode == Keyboard.DOWN )
    			{
    				Downkey=false;
    			}
    			
    			if( keyboardEvent.keyCode == Keyboard.UP )
    			{
    				Upkey=false;
    			}
    			
    			if( keyboardEvent.keyCode == Keyboard.LEFT )
    			{
    				Leftkey=false;
    			}
    			
    			if( keyboardEvent.keyCode == Keyboard.RIGHT )
    			{
    				Rightkey=false;
    			}
    		}
    	}
    }

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