Keep getting collision errors


#1

i am following Zachary Lewis’s tutorial, Making Games With FlashPunk: Episode 2 - Adding Players and Enemies, and i got to the point where i need to handel collision i type the code in the video i get an error saying:

"C:\Users\???\Documents\RGB\src\Player.as(24): col: 8 Error: Call to a possibly undefined method collide.
		 if (collide("enemy", x, y)) {
		     ^
C:\Users\????\Documents\RGB\src\Player.as(24): col: 25 Error: Access of undefined property x.
		 if (collide("enemy", x, y)) {
		                      ^
C:\Users\????\Documents\RGB\src\Player.as(24): col: 28 Error: Access of undefined property y.
		 if (collide("enemy", x, y)) {
		                         ^
C:\Users\????\Documents\RGB\src\Player.as(25): col: 11 Error: Access of possibly undefined property color through a reference with static type Class.
		  Image.color = 000000
		        ^
C:\Users\????\Documents\RGB\src\Player.as(28): col: 9 Error: Access of possibly undefined property color through a reference with static type Class.
		Image.color = 0xff0000
		      ^
Build halted with errors (fcsh)."

i back to double and triple check if i missed something but i can’t figure out what.

package 
{
import net.flashpunk.Entity;
import net.flashpunk.graphics.Image;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;
import net.flashpunk.FP;
	public class Player extends Entity
	{  [Embed(source = "../assets/R.png")]private const PLAYER:Class;
	    public var  image:Image;
		public function Player()
		{
          graphic = new Image(PLAYER);
		  setHitbox(20, 20, 0, 0);
		  type = "player";
		}
		override public function update():void 
		{
			if (Input.check(Key.LEFT)) { x -= 5; }
			if (Input.check(Key.RIGHT)) { x += 5; }
			if (Input.check(Key.UP)) { y -= 5; }
			if (Input.check(Key.DOWN)) { y += 5; }
		}
		 if (collide("enemy", x, y)) {
		  Image.color = 000000
		  }
		else(
		Image.color = 0xff0000
		)
      }	
	 
		super.update();
}

I know my code isn’t exactly the same as the video but im trying to make my own game based of it.


(Zachary Lewis) #2

Just off the bat, I’m noticing that you’re not using the right variable name for your image. Actionscript is case-sensitive, so instead of using Image.color, you should be using image.color. This change will fix your errors on line 25 and 28.

Also, it looks like you’ve added a } too early. Remove the } before your call to collide, since it currently exists outside a function.


#3

Ok did that and then i got this message “[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.” CHANGED CODE: package { import net.flashpunk.Entity; import net.flashpunk.graphics.Image; import net.flashpunk.utils.Input; import net.flashpunk.utils.Key; import net.flashpunk.FP; public class Player extends Entity { [Embed(source = “…/assets/R.png”)]private const PLAYER:Class; public var image:Image; public function Player() { graphic = new Image(PLAYER); setHitbox(20, 20, 0, 0); type = “player”; } override public function update():void { if (Input.check(Key.LEFT)) { x -= 5; } if (Input.check(Key.RIGHT)) { x += 5; } if (Input.check(Key.UP)) { y -= 5; } if (Input.check(Key.DOWN)) { y += 5; }

	 if (collide("enemy", x, y)) {
	  image.color = 000000
	  }
	else{
	image.color = 0xff0000
	}}
  }	

}


(Martí Angelats i Ribera) #4

I feel that you really need to use a clear programing. This code is really hard to read (not even talking that it doesn’t work), so i hardly suggest to stick in a way and follow it.

Using MY way of coding and correcting the errors this is how it looks like (didn’t change the code)

package 
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	import net.flashpunk.FP;
	
	public class Player extends Entity
	{
		[Embed(source = "../assets/R.png")]
		private const PLAYER:Class;
		
		public var image:Image;
		
		public function Player()
		{
			super(0, 0); //initial x and y
			image = new Image(PLAYER);
			graphic = image;
			setHitbox(20, 20, 0, 0);
			type = "player";
		}
		
		override public function update():void 
		{
			if (Input.check(Key.LEFT)) { x -= 5; }
			if (Input.check(Key.RIGHT)) { x += 5; }
			if (Input.check(Key.UP)) { y -= 5; }
			if (Input.check(Key.DOWN)) { y += 5; }
			
			if (collide("enemy", x, y))
			{
				image.color = 0x000000;
			}
			else
			{
				image.color = 0xFF0000;
			}
			
			super.update();
		}
	}	
}

Here there were multiple errors:

  1. As @zachwlewis said, Image should be image
  2. A general desorganization making some code be outside the function making no sance (the main cause of the error).
  3. Not initializing the Player class.
  4. image was never initialized so it was undefinied (would had caused an error after solving the other ones).

PD: For me image should be a private function becouse noone outside the class should call it. This is not an error but it’s a good thing to keep in mind [the atributes visibility].


#5

Oh Thank you guys so much for the help i was getting so frustrated with this.


(Zachary Lewis) #6

When did @jacobalbano say that? :stuck_out_tongue:


#7

yea i was wondering about that


(Martí Angelats i Ribera) #8

ups… I’m so sorry. I don’t know what i was thinking >.<


#9

Ok i tried that code and i got this error. “Error: null Build halted with errors (fcsh).”


(Martí Angelats i Ribera) #10

It is not an error of the code (at least this class). Try rebuilding the project.


#11

what should i change? World class:

package 
{
import net.flashpunk.World;
import net.flashpunk.Entity;
import flash.display.BitmapData;
import net.flashpunk.graphics.Image;
import net.flashpunk.utils.Input;
import net.flashpunk.FP;
	public class MyWorld extends World
	{
		private var square:Entity;
		public function MyWorld()
		{
           add(new Player());
		   add(new EnemyA());
		   add(new EnemyA());
		}
	}
}

Enemy class:

package 
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.FP;
	public class EnemyA extends Entity 
	{
		[Embed(source = "C:/Users/****/Documents/RGB/assets/BLK.png")] public const ENEMY_A:Class
		public function EnemyA() {
			graphic = new Image(ENEMY_A);
			x = FP.rand(FP.screen.width);
			y = FP.rand(FP.screen.height);
			type = "enemy";
			setHitbox(20, 20, 0, 0);
			}
	}
	
}

Main class:

package
{
import net.flashpunk.Engine;
import net.flashpunk.FP;
public class Main extends Engine
{
	public function Main()
	{
		super(800, 600, 60, false);
		FP.world = new MyWorld
		FP.console.enable();
	}
	override public function init():void {
		trace("Flashpunk has started successfully!");
	}
}

}


(Jacob Albano) #12

Everyone knows @zachwlewis and I are the same person, we just don’t advertise the fact.

@trueitachi I see your original post uses < pre > tags; if you put ``` (three backticks) before and after, it’ll show up better.


(Martí Angelats i Ribera) #13

In the constructor of the main class you forgot a semicolon (;).


#14

Oops simple mistake…i added it but this happened: C:\Users****\Documents\RGB\src\Main.as(10): col: 4 Error: A super statement can be used only inside class instance constructors.


(Martí Angelats i Ribera) #15

try this:

package
{
import net.flashpunk.Engine;
import net.flashpunk.FP;

public class Main extends Engine
{
    public function Main()
    {
        super(800, 600, 60, false);
    }
    
    override public function init():void
    {
        FP.world = new MyWorld;
        FP.console.enable();
        trace("Flashpunk has started successfully!");
    }
}

}

And remember to make this class the main one.


#16

thanks for the help it finally worked. :grinning: :grin:


(Martí Angelats i Ribera) #17

I just noticed something improvable in your world class. You should add entities in the begin() function not in the contructor (otherwise might generate an error).

package
{
import net.flashpunk.World;

public class MyWorlds extends World
{
    //no need for contructor
    override public function begin():void
    {
        add(new Player);
        add(new Enemy);
        add(new Enemy);
    }
}

}

#18

Oh thank you i will try it if i run into anymore trouble.


(Zachary Lewis) #19

I formatted that post. I opted for pre for the output because it isn’t code. :blush: