Pergunta

When I have this code, it comes up with this error "1061: Call to a possibly undefined method hitTestPoint through a reference with static type Class."

package  {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;

public class Code extends MovieClip {

    var charSpeed:int = 0;
    var velocity:int = 0;
    var gravity:Number = 1;
    var Jump:Boolean = false;


    public function startGame(){
         stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
         stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
         stage.addEventListener(Event.ENTER_FRAME, loop);
     }

    public function Code() {
        // constructor code
    }

    function checkKeyDown(evt:KeyboardEvent){
        if (evt.keyCode == Keyboard.LEFT){
            charSpeed -= 10;
        }
        if (evt.keyCode == Keyboard.RIGHT){
            charSpeed += 10;
        }
        if (evt.keyCode == Keyboard.DOWN){
            if(!Jump){
                velocity -= 14;
                Jump = true;
            }
        }
    }

    function checkKeyUp(evt:KeyboardEvent){
        if (evt.keyCode == Keyboard.LEFT){
            charSpeed = 0;
        }
        if (evt.keyCode == Keyboard.RIGHT){
            charSpeed = 0;
        }
    }

    function loop(evt:Event){
        player.x = velocity;
        if (player.x < 0){
             player.x = 0;
        }
        if (player.x > 550){
             player.x = 550;
        }

        velocity += gravity;

        if (!platform.hitTestPoint(player.x, player.y, true)){
            player.y += velocity;
        }

        for (var i = 0; i < 10; i++){
            if (platform.hitTestPoint(player.x, player.y, true)){
                player.y--;
                velocity = 0;
                Jump = false;
            }
        }
    }
}
}

The option I had was adding a MovieClip variable to my platform, and that did remove this error, but I wasn't sure how to link my actual instance of platform to my variable. Oh and by the way the platform's instance is platform. If anyone has any clues on how to fix this error, that would be great.

Foi útil?

Solução

You may set platform value in the constructor

public function class Code {

  //assume Platform is the class type,remember to import it
   private var platform:Platform;

   public function Code(value:Platform) {
       platform = value;
   }

}

When you create Code instance

var code:Code = new Code(platform);//platform is the instance
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top