PDA

View Full Version : Class instance objects not colliding (gskinner's collision)



kilamantok
February 6th, 2009, 07:09 AM
Thank you very much for giving some time to read my problem.

Im building a shooter game where a bullet is called from a class and the enemy is also called from a class.

I cant make them collide. In order for stuff to start exploding.

I keep getting this error

1120:Access of undefined property nBullet
//This one creates a meteor by calling class Meteor every 2 seconds

var meteorTimer:Timer=new Timer(2000);
meteorTimer.start();
meteorTimer.addEventListener(TimerEvent.TIMER, bulletMeteor, false , 0 , true);

function bulletMeteor(evt:TimerEvent):void {

var nMeteor:Meteor= new Meteor();
nMeteor.x=800;
nMeteor.y=Math.random()*380+10;
addChild(nMeteor);
nMeteor.addEventListener(Event.ENTER_FRAME, rMeteor);
function rMeteor(evt:Event):void {
if (nMeteor.x<=-10) {
removeChild(nMeteor);
nMeteor.removeEventListener(Event.ENTER_FRAME, rMeteor);
}
if (nMeteor.hitTestObject(nBullet)) {
trace("collision detected");
removeChild(nMeteor);
nMeteor.removeEventListener(Event.ENTER_FRAME, rMeteor);
}
if (PixelPerfectCollisionDetection.isColliding(nMeteo r,nBullett,stage,true,255)) {

trace("collision detected");
removeChild(nMeteor);
nMeteor.removeEventListener(Event.ENTER_FRAME, rMeteor);
}
}
}

//__________________________________________________ __________________________________________________ _


// This one creates a bullet by calling class Bullet

stage.addEventListener(MouseEvent.CLICK, addBullet);
function addBullet(evt:MouseEvent):void {
var xSpeed:Number =(hTarget/radius)*-10;
var ySpeed:Number =(dTarget/radius)*10;
var nBullet:Bullet = new Bullet();
nBullet.x=centerX;
nBullet.y=centerY;
nBullett.rotation=angle;
addChildAt(nBullet,0);
addEventListener(Event.ENTER_FRAME, moveBullet);
function moveBullet(evt:Event):void {
nBullet.x+=xSpeed;
nBullet.y+=ySpeed;
if (nBullet.y<0||nBullet.y>400||nBullet.x<0||nBullet.x>801) {
removeChild(nBullet);
removeEventListener(Event.ENTER_FRAME, moveBullet);
}
}
}any kind of help will be greatly appreciated!

rrh
February 6th, 2009, 01:27 PM
It looks like you need to better understand variable scope. As it stands, when rMeteor is called, it won't have access to the same values of nMeteor and nBullet as they have been assigned in other functions.

You're probably going to need an array for the bullets and for the meteors and then add and remove them from that array as needed. Then you can loop through all the entries of the arrays and test them against each other.