Forum Topic: Programming question

(126 views • 6 replies)

This topic is 1 page long.

<< < > >>
None

bcapecci

Reply To Post Reply & Quote

Posted at: 6/27/09 08:06 PM

bcapecci NEUTRAL LEVEL 06

Sign-Up: 03/07/08

Posts: 271

Having trouble with procedural programming collision detection. I made my own detection that successfully uses the function colliding to check pixel-perfect collision.

I need to check if the player is colliding with the shots made by the enemy during the tutorial phase...

//Not all code is shown 
//------------------------------
var timer:Timer = new Timer(2000);
timer.addEventListener(TimerEvent.TIMER, testShots);
timer.start();

function testShots(e:TimerEvent):void {
       var lightShot:LightShot = new LightShot();
	addChild(lightShot);
	lightShot.x = lightTester.x;
	lightShot.y = lightTester.y;
	lightShot.addEventListener(Event.ENTER_FRAME, testBullet);
}

function testBullet(event:Event):void {
	event.target.y += 3;
}

//within enterframe event obviously
if (collisiondetection.colliding(player, lightShot, 1))
       do something...

Really simple code but I cannot get it to work.

It wont recognize lightShot because it is inside a different method. A fix would be to make the varible public but I cannot say the variable is public because its programmed on the first frame of the timeline, often called procedural/functional programming as opposed to OOP.

If I put the "new LightShot();" declared outside the method itself the collision test works great however only 1 lightshot bullet can be on the screen at the same time! Thus every time the timer method is called the addChild rebounds the current bullet back to the coordinates specified.

Thank you, Brandon


None

Woadraiders

Reply To Post Reply & Quote

Posted at: 6/27/09 08:47 PM

Woadraiders DARK LEVEL 11

Sign-Up: 11/11/07

Posts: 561

Create an empty array outside of all other functions, push a bullet to this array when its created, and splice it out when it's finished.
You'll need a for loop to go through each bullet and see if it hit anything.

And why aren't you using OOP? Especially considering that you're scripting in as3.

Mibbygames.com
panterA

BBS Signature

None

bcapecci

Reply To Post Reply & Quote

Posted at: 6/27/09 08:56 PM

bcapecci NEUTRAL LEVEL 06

Sign-Up: 03/07/08

Posts: 271

Thank you. Its a good idea, I've done that before in a particle engine. Why am I not using OOP? Good question, haha. I should be. Anyhow, Ill either do that or put all the code in the document class which I believe can have a package keyword and thus use public.


None

Woadraiders

Reply To Post Reply & Quote

Posted at: 6/27/09 09:00 PM

Woadraiders DARK LEVEL 11

Sign-Up: 11/11/07

Posts: 561

Lol, that's better than no OOP at all I guess. Good luck with your game

Mibbygames.com
panterA

BBS Signature

None

bcapecci

Reply To Post Reply & Quote

Posted at: 6/29/09 11:24 AM

bcapecci NEUTRAL LEVEL 06

Sign-Up: 03/07/08

Posts: 271

Made a array and still doesnt work. Detects the first bullet created and then none after. Did I do something wrong because when I traced bulletArray there is should be 2 objects in the array except for the first 2 seconds, instead after the first 2 bullets and 2 objects in the array there becomes 1 for the rest of the time. Take a look at the code:

var collisiondetection:Collision=new Collision();
var bulletArray:Array = new Array();

var timer:Timer = new Timer(2000);
timer.addEventListener(TimerEvent.TIMER, testShots);
timer.start();

function testShots(e:TimerEvent):void {
	var darkShot:DarkShot = new DarkShot();
	addChild(darkShot);
	bulletArray.push(darkShot);
	darkShot.x = darkTester.x;
	darkShot.y = darkTester.y;
	darkShot.addEventListener(Event.ENTER_FRAME, testBullet);
}

function testBullet(event:Event):void {
		event.target.y += 3;
		if (event.target.y >= 400)
		{
			event.target.removeEventListener(Event.ENTER_FRAME, testBullet);
			this.removeChild(DisplayObject(event.target));
			bulletArray.splice(event.target);
		}
}

function enterFrameFunction(Event) {
	for (var i:uint=0; i<bulletArray.length-1; i++)
	{
		if (collisiondetection.isColliding(player, bulletArray[i], 1)){
			trace("collision");
		}
	}
}

stage.addEventListener(Event.ENTER_FRAME, enterFrameFunction);

None

Woadraiders

Reply To Post Reply & Quote

Posted at: 6/29/09 02:08 PM

Woadraiders DARK LEVEL 11

Sign-Up: 11/11/07

Posts: 561

Well, Array.splice takes two parameters, and you didn't get either of them right.
Array.splice(startIndex:Number, length:Number);

What you should do is

var collisiondetection:Collision=new Collision();
var bulletArray:Array = new Array();

var timer:Timer = new Timer(2000);
timer.addEventListener(TimerEvent.TIMER, testShots);
timer.start();

function testShots(e:TimerEvent):void {
	var darkShot:DarkShot = new DarkShot();
	addChild(darkShot);
	bulletArray.push(darkShot);
	darkShot.x = darkTester.x;
	darkShot.y = darkTester.y;
}


function enterFrameFunction(Event) 
{
	for (var i:uint=0; i<bulletArray.length; i++)
	{
		bulletArray[i].y += 3;
		if(bulletArray[i].y >= 400)
		{
			this.removeChild(DisplayObject(bulletArray[i]));
			bulletArray.splice(i,1);
			i--;
			continue;
		}
		if (collisiondetection.isColliding(player, bulletArray[i], 1))
		{
			trace("collision");
		}
	}
}

stage.addEventListener(Event.ENTER_FRAME, enterFrameFunction);

You could take this a step furthur, and create a DarkShot class that has a function called Update():Boolean. So your enterFrameFunction would look something like this:

function enterFrameFunction(e:Event):void
{
	for (var i:uint=0; i<bulletArray.length; i++)
	{
		if(bulletArray[i].Update)
		{
			this.removeChild(DisplayObject(bulletArray[i]));
			bulletArray.splice(i,1);
			i--;
			continue;
		}
		if (collisiondetection.isColliding(player, bulletArray[i], 1))
		{
			trace("collision");
		}
	}
}

.... Inside the DarkShot Code

public function Update():Boolean
{
	this.y += 3;
	if(this.y >= 400)
	{
		return true;
	}
	return false;
}

Mibbygames.com
panterA

BBS Signature

None

bcapecci

Reply To Post Reply & Quote

Posted at: 6/30/09 08:45 AM

bcapecci NEUTRAL LEVEL 06

Sign-Up: 03/07/08

Posts: 271

Thanks a bunch woadraiders. Long posts like that dont help your post count but certainly make people with errors very happy :)


All times are Eastern Standard Time (GMT -5) | Current Time: 02:48 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!