Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Can anyone tell me what I did wrong?

New Here ,
Apr 11, 2015 Apr 11, 2015

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

Screenshot (101).png

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



TOPICS
ActionScript
270
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 11, 2015 Apr 11, 2015

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 12, 2015 Apr 12, 2015

  //Touch the car

  if (mcVehicle.hitTestObject(mcPlayer.hitBox))

  {

  //Hearts += 1;

  gameOver();

  gotoAndPlay("end");

  }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 12, 2015 Apr 12, 2015

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 13, 2015 Apr 13, 2015

ReferenceError: Error #1065: Variable Platform is not defined.

  at TouchALife/setupGame()

  at TouchALife/startGame()

  at TouchALife/frame1()

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 13, 2015 Apr 13, 2015
LATEST

If you make a reference to the Platform class you have to import it so that the compiler knows of it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines