0

I am trying to create a platformer game and i am trying to make "player1" stop when it hits a "platform". here is my code so far,

    gotoAndStop("gameStart");

import flash.display.MovieClip;
import flash.events.*;
import flash.ui.Keyboard;
import flash.ui.*;


import flash.utils.Timer;
import flash.events.TimerEvent;

player1.gotoAndStop("nothing");


 //private var speed:Number = 0;
 //private var maxspeed:Number = 4;

var myTimer:Timer = new Timer(10,0);


stage.focus = this;


player1.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
/*
myTimer.addEventListener(TimerEvent.TIMER,someFunction);



myTimer.start();

function someFunction(event:TimerEvent) {
 player1.y += 2;


}
*/

function setup() {
 stage.addEventListener(KeyboardEvent.KEY_DOWN, reactToArrowKeys);
}
setup();


function reactToArrowKeys(keyEvent:KeyboardEvent) {

 if (keyEvent.keyCode == 37) {

  if (player1.x > 0) {
   player1.x -= 5;
  }

 } else if (keyEvent.keyCode == 39) {

  if (player1.x < 700) {
   player1.x += 5;
  }

 }


}


function enterFrameHandler(e:Event):void {
   if (player1.hitTestObject(platform)) {
    trace("hitting");
   } else {
    player1.y += 4;
   }
  }

however the hitTestObject function (enterFrameHandler) does not work properly and will always take the "else" route.

please help!

1
  • can you supply any more information? because it works for me. I created a MovieClip called player1 and a brick called platform and it detects the hit. Commented Aug 26, 2009 at 5:31

1 Answer 1

1

The code as posted works fine for me. I'd look for some other kind of silly mistake - for example, if you copied and pasted movie clips, you might have more than one clip on the stage named "platform", in which case your reference may not resolve to the one you intend. Or something else along those lines.

To track it down, try calling:

trace( player1.getBounds(stage) );
trace( platform.getBounds(stage) );

which will tell you where Flash thinks the bounding boxes of those clips are. My guess is that code will return something other than what you'd expect, and resolving that discrepancy will show where the bug is.

Sign up to request clarification or add additional context in comments.

2 Comments

ooooooh ok i didnt know you couldnt have more than on clip named platform! thanks!
Yeah, if you do, when you refer to "platform" Flash just picks one of them. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.