Hi all… I have 4 hours to work out why my code has stopped working and I really need your help. I’ve been getting this Error and I can’t seem to be rid of it. I have trawled the internet for answers (including this site) and I just can’t seem to find a way to make this work.
Basically, I am teaching kids to make a little game in flash. The idea is to set up collisions so that when the MainCharacter hits an Enemy (BlueBlock) it sends the player to a little loop on the time line saying hit! or whatever. Last week I did this and had 4 blue blocks to avoid and all was working fine. I have since added another 4 blocks and some code to restrain the boundaries so the MainCharacter doesn’t fly out… but now I’m getting the Error “#2007: Parameter hitTestObject must be non-null.” It’s driving me up the wall because even after taking out all the extra stuff i added it doesn’t work again.
//add the event listener to listen when a key is pressedstage.addEventListener(KeyboardEvent.KEY_DOWN, CharacterMove);
//add the function to tell the object which direction to move and how far
function CharacterMove(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.UP :
MainCharacter.y-=10;
break;
case Keyboard.DOWN :
MainCharacter.y+=10;
break;
}
//controls the boundaries of the stage
if (MainCharacter.y<0) {
MainCharacter.y=0;
}
else if (MainCharacter.y > 400-73) {
MainCharacter.y=400-73; //height of the stage minus the hieght of the character
}
}
//add the event listener to listen when a collision is detected
stage.addEventListener(Event.ENTER_FRAME, checkCollision);
//add the function to check if the objects collide
function checkCollision(event:Event) {
//test the mainCharacter to see if it overlaps a block
if (MainCharacter.hitTestObject(BlueBlock)) {
gotoAndPlay("end");
}
if (MainCharacter.hitTestObject(BlueBlock2)) {
gotoAndPlay("end");
}
if (MainCharacter.hitTestObject(BlueBlock3)) {
gotoAndPlay("end");
}
if (MainCharacter.hitTestObject(BlueBlock4)) {
gotoAndPlay("end");
}
if (MainCharacter.hitTestObject(BlueBlock5)) {
gotoAndPlay("end");
}
if (MainCharacter.hitTestObject(BlueBlock6)) {
gotoAndPlay("end");
}
if (MainCharacter.hitTestObject(BlueBlock7)) {
gotoAndPlay("end");
}
if (MainCharacter.hitTestObject(BlueBlock8)) {
gotoAndPlay("end");
}
}
I realise that this code is messy but it needs to stay relatively in tact as it’s part of my teaching. Thanks in advance.