Collision / World Class


(I'll tell you what I want, what I really really want) #1

Yo.

I’m making loads of different levels in the latest project, and in every one of them, I’d like them to have different events based on collisions. Let’s say, if in this level you hit this with that, you go to the next level. And for example, I wrote this just now, seems simple, why isn’t this working?

	private var _bullet:Bullet;
	private var _navyBlock:navyMove;

		if (_bullet.collide("navyMove"))
		{
			FP.world = LevelTwo();
		}

(Jacob Albano) #2

Are your types set correctly?


(I'll tell you what I want, what I really really want) #3

navyBlock entity’s class:

type = “navyMove”;

I think they are.


(Jacob Albano) #4

Hmm, okay.

  • Where is the code above being run?

  • Do both entity types have a hitbox or other mask?

  • Are both entity types collidable?


(I'll tell you what I want, what I really really want) #5

World’s Class Update Function.

Both with hitboxes,

Collidable = true, in entity’s main function.


(Justin Wolf) #6

The collide function requires an x and y parameter after the type parameter. Try the following:

if (_bullet.collide("navyMove", _bullet.x, _bullet.y))
{
     FP.world = new LevelTwo();
}

Is your compiler (FlashDevelop?) not shooting out error messages to you when you attempt to compile the game? It should say that the collide function requires three parameters upon compiling and then just simply fail.


(I'll tell you what I want, what I really really want) #7

It doesn’t, it just throws a white screen (yes, I’ve tried to fix it multiple times) and the console log is saying nothing.

@justinwolf, not working bro :frowning:


(Jacob Albano) #8

Bro, you need to fix your compiler.

Just to be clear, is anything working prior to this point? or is it just compiling a white screen and never running anything?


(I'll tell you what I want, what I really really want) #9

Without override public function update():void { super.update();

		private var _bullet:Bullet;
		private var _pill:Pill;
		
		if (_bullet.collide("pill", _bullet.x, _bullet.y))
		{
			 FP.world = new Menu1();
		}			
	}

it works like a well oiled minimalist 2d machine.


(I'll tell you what I want, what I really really want) #10

FIXED THE WHITE SCREEN, EVERYBODY HALLELUJAH! (there’s a new version of FlashDevelop). Now I can be a normal programmer, not a white-screen-throwing-mofo-builder kinda dude.

And I know what’s wrong now:

@jacobalbano, @justinwolf, any ideas?


(Justin Wolf) #11

Looks as if your _bullet object doesn’t actually exist. Where are you creating _bullet?


(I'll tell you what I want, what I really really want) #12

In my world’s update function.


(I'll tell you what I want, what I really really want) #13

Since it seems like nobody knows how to solve my problem (so far), could anyone tell me, how they would do it? Let’s say, you have a bullet, and a spinning square. If bullet hits the square, it triggers the new world to initialize. How do I do that, apart from my noobish technique?

Sry for the double post.


(azrafe7) #14

As @justinwolf already pointed out, the error you get on line 89 (Cannot access a property or method of a null object reference.) most probably indicates that your _bullet var is null at some point.

To fix it you can try to also test for its existence in the if condition:

  if (_bullet != null && _bullet.collide("pill", _bullet.x, _bullet.y)

(I'll tell you what I want, what I really really want) #15

this is the bullet’s class:

	public function Bullet(posx:int, posy:int) 
	{
		graphic = new Image(BULLET_ART);
		collidable = true;
		
		x = posx;
		y = posy;
		setHitbox(10, 10, 5, 5);
		
		type = "bullet";
		
	}

I can’t see why is it working. It doesn’t make any sense.


(Justin Wolf) #16

Can you post the code that creates your bullet?


(I'll tell you what I want, what I really really want) #17
		var bullet:Bullet = new Bullet(x - 3, y);
		
		if (Input.released(Key.S))
		{
			world.add(bullet);
			bullet.fire(0, 5);
		}
		if (Input.released(Key.W))
		{
			world.add(bullet);
			bullet.fire(0, -5);
		}
		if (Input.released(Key.A))
		{
			world.add(bullet);
			bullet.fire(-5, 0);
		}
		if (Input.released(Key.D))
		{
			world.add(bullet);
			bullet.fire(5, 0);
		}

(Justin Wolf) #18

You’re creating bullet and referencing _bullet.


(I'll tell you what I want, what I really really want) #19
	 if  (bullet.collide("pill", bullet.x, bullet.y))
	{
		FP.world = new Menu1;
	}

well, it’s still not working.


(Justin Wolf) #20

Hmm. Post the entire source of your world class so I can further examine what could be happening.