Copy link to clipboard
Copied
I've been trying to code a game and it kept on giving me "TypeError: Error #1009" on the output whenever I make my player touch the car.
Here's the .as codes:
package {
import flash.display.*;
import flash.events.*;
import flash.utils.*;
import Game.*;
public class TouchALife extends MovieClip
{
private var Hearts:Number;
private var jumping, left, right:Boolean;
private var speedX, speedY: Number;
private var platformsArray, heartsArray:Array;
public function TouchALife()
{
}
public function startGame()
{
stage.focus = stage;
Hearts = 0;
speedX = 0;
speedY = 0;
jumping = false;
left= false;
right = false;
platformsArray = new Array();
heartsArray = new Array();
setupGame();
addEventListener(Event.ENTER_FRAME,update);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
}
private function setupGame()
{
//Check for items on the stage and put them into
//the appropriate array
for (var i=0; i< MovieClip(root).numChildren; i++)
{
var object = MovieClip(root).getChildAt(i);
if (object is Platform)
{
platformsArray.push(object);
}
else if (object is Heart)
{
heartsArray.push(object);
}
}
}
private function keyDownHandler(evt:KeyboardEvent)
{
if (evt.keyCode == 32) //spacebar
{
//Make player jump
jumping = true;
}
else if (evt.keyCode == 37)
{
//Move player left
left = true;
}
else if (evt.keyCode == 39)
{
//Move player right
right = true;
}
}
private function keyUpHandler(evt:KeyboardEvent)
{
if (evt.keyCode == 37)
{
//Stop Moving Left
left = false;
}
else if (evt.keyCode == 39)
{
//Stop Moving Right
right = false;
}
}
public function update(evt:Event)
{
//This is the game loop
//Handle user input
if (right)
{
mcPlayer.moveRight();
}
else if (left)
{
mcPlayer.moveLeft();
}
else
{
mcPlayer.stopMoving();
}
if (jumping && !mcPlayer.isInAir())
{
mcPlayer.jump();
}
//reset jump
jumping = false;
//Handle game logic
mcPlayer.update();
//Check for collision between player and platforms
if (mcPlayer.isFallingDown())
{
for (var i = platformsArray.length - 1; i >= 0; i--)
{
if (platformsArray.hitTestObject(mcPlayer.hitBox))
{
mcPlayer.y = platformsArray.y;
mcPlayer.hitFloor(platformsArray);
//Exit the loops
break;
}
}
}
//Touch the car
if (mcVehicle.hitTestObject(mcPlayer.hitBox))
{
//Hearts += 1;
gameOver();
gotoAndPlay("end");
}
//Check for collision between player and aliens
for (i = heartsArray.length - 1; i >= 0; i--)
{
if (heartsArray.hitTestObject(mcPlayer.hitBox))
{
if (mcPlayer.isFallingDown())
{
//player jumped on it
removeChild(heartsArray);
heartsArray.splice(i,1);
Hearts += 1;
}
else
{
//player is hit
removeChild(heartsArray);
heartsArray.splice(i,1);
Hearts += 1;
}
}
}
//Handle display
txtHearts.text = String(Hearts);
}
private function resetGame()
{
mcPlayer.x = 410;
mcPlayer.y = 575;
}
private function gameOver()
{
removeEventListener(Event.ENTER_FRAME,update);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
}
}//end class
}//end package
Here's how my .fla timeline looks like
At frame 1:
startGame();
stop ();
So here's what I'm actually planning to do: No matter how many hearts the player decides to collect, as long as they touch the car, there'll be an animation playing after that.
But it's just that when the player touches the car, the type error starts coming out from the Output
Copy link to clipboard
Copied
What portion of the code you show involves the player touching the car? I cannot readily see anything of the sort. If the player or car gets removed as a result of the hit then that might be the source of the problem.
The 1009 error indicates that one of the objects being targeted by your code is out of scope. This could mean that the object....
- is declared but not instantiated
- doesn't have an instance name (or the instance name is mispelled)
- does not exist in the frame where that code is trying to talk to it
- is animated into place but is not assigned instance names in every keyframe for it
- is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).
If you go into your Publish Settings Flash section and select the option to Permit debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.
Copy link to clipboard
Copied
//Touch the car
if (mcVehicle.hitTestObject(mcPlayer.hitBox))
{
//Hearts += 1;
gameOver();
gotoAndPlay("end");
}
Copy link to clipboard
Copied
either mcPlayer.hitBox is probably null.
to confirm that's the line of code causing the error click file>publish settings>swf>and tick 'permit debugging'. retest.
the problematic line number will be in the error message. use the trace function to further pinpoint the null object.
and then bookmark:
Debugging ActionScript 3.0 Errors, http://forums.adobe.com/docs/DOC-2542;
Debugging AcriptScript That Triggers No Errors, http://forums.adobe.com/docs/DOC-2572;
Copy link to clipboard
Copied
ReferenceError: Error #1065: Variable Platform is not defined.
at TouchALife/setupGame()
at TouchALife/startGame()
at TouchALife/frame1()
Copy link to clipboard
Copied
If you make a reference to the Platform class you have to import it so that the compiler knows of it.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now