1

我正在创建简单的 Flash 游戏。思路:绿色角色通过点击(左/右箭头)在地面上行走,点击空格键角色必须跳跃。目前角色留在地上,行走,但不与墙壁发生碰撞(穿过墙壁)。在这部分代码中,我尝试与 ground3 墙发生碰撞,但它不起作用,角色穿过墙,如果我跳到 ground3(留在顶部)我不能向右移动,因为它接触到 ground3 . 如果角色与墙壁右侧接触,我需要这样做,然后将速度设置为 0。

这是代码的一部分(向右移动):

 if(right){

            Hero.x_speed = walkspeed;
            setDirection(0);
            trace("right");

/////////////////////SOMETHING WRONG HERE//////////////////////////////
            if (ground3.hitTestPoint((centrasXright), centrasY, true)){
                trace("HIT THE WALL/GROUND with RIGHT"); //always get this message when stay on the ground 3
                Hero.x_speed = 0;
            }
            else {
                trace("Do not touch anything");

            }

这是我的游戏照片: 游戏

这是我的变量:

private var gravity:Number = 0.01;
private const max_speed:int = 8;

private const walkspeed:int = 4;
private const jumpspeed:int = 10;

private const start_x:int = 50;
private const start_y:int = 300;

private var left:Boolean;
private var up:Boolean;
private var right:Boolean;
private var space:Boolean;

private var ground:Ground = new Ground;
private var Hero:hero = new hero;

这是我的代码的一部分:

private function create_hero(){
            addChild(Hero);
            Hero.x = start_x;
            Hero.y = ground.y-30;
            Hero.x_speed = 0;
            Hero.y_speed = 0;


        }
        private function setDirection(param) {
    if (param == 0) {
        Hero.scaleX = 1;
    } else {
        Hero.scaleX = -1;
    }
}

        private function update_hero(){
            Hero.y_speed += gravity;

            if(left){
                Hero.x_speed = -walkspeed;
                setDirection(1);
            }
                var test:MovieClip = new MovieClip;
                test.x = Hero.x;
                test.y = Hero.y;
                var centrasXright:Number = test.x + (Hero.width/2);
                var centrasXleft:Number = test.x - (Hero.width/2);
                var centrasY:Number = test.y + (Hero.height/2);
            if(right){

                Hero.x_speed = walkspeed;
                setDirection(0);
                trace("right");

                if (ground3.hitTestPoint((centrasXright), centrasY, true)){
                    trace("HIT THE WALL/GROUND with RIGHT");
                    Hero.x_speed = 0;
                }
                else {
                    trace("Do not touch anything");

                }


            }
            if(up && Hero_col.can_jump){
                Hero.y_speed = -jumpspeed;


            }

            if(Hero.y_speed > max_speed){
                Hero.y_speed = max_speed;
            }
            ///////////////////////////////////////////////////////////////////
            ////////////////HERE IS COLLISION WITH THE TOP OF GROUND///////////
                        ///////////////////////////////////////////////////////////////////
            if(Hero.y_speed>0 && Hero.hitTestObject(ground) || Hero.hitTestObject(ground3) ){
                        Hero.y_speed=0;

                if(space){
                trace("Clicked SPACE");
                Hero.y_speed += gravity;
                Hero.y -= 80;
                }
                else {

                }


 }

            forecast_y = Hero.y + Hero.y_speed;
            forecast_x = Hero.x + Hero.x_speed;

            Hero_col.solve_all(forecast_x, forecast_y);


            Hero.x_speed = 0;
        }
4

0 回答 0