【问题标题】:Action Script 3. How to make floor on the game stage?动作脚本 3. 如何在游戏舞台上铺地板?
【发布时间】:2013-09-12 07:46:56
【问题描述】:

我正在创建简单的 Flash 游戏,我只是制作了角色控件(向左、向右和跳跃)。当我添加重力时,我的角色会从舞台上掉下来。

我已经画了地板,导入到库中,但我不知道代码应该是什么样子。

我的楼层叫做“Ground”,我添加了 AS Linkage“Ground”。

目前我的代码如下所示: (注:我的角色名为“英雄”)

public class Script extends MovieClip{              // start the script

private const gravity:int = 1;
private const max_speed:int = 8;

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

private var forecast_x:int;
private var forecast_y:int;

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

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

private var level:Array = new Array();

private var Map_data:Data = new Data;               // create a version of the Data.as
private var Hero_col:collision_manager = new collision_manager;

private var Hero:hero = new hero;

    public function Script(){                       // the init (will only be runned once)
        //BuildMap();
        create_hero();
        addEventListener(Event.ENTER_FRAME, main);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
        stage.addEventListener(KeyboardEvent.KEY_UP, key_up);

        Hero_col.Setup(25,level,Hero);
    }

    private function main(event:Event){
        update_hero();
    }

    private function key_down(event:KeyboardEvent){
        if(event.keyCode == 37){
            left = true;
        }
        if(event.keyCode == 38){
            up = true;
        }
        if(event.keyCode == 39){
            right = true;
        }
    }

    private function key_up(event:KeyboardEvent){
        if(event.keyCode == 37){
            left = false;
        }
        if(event.keyCode == 38){
            up = false;
        }
        if(event.keyCode == 39){
            right = false;
        }
    }

        private function create_hero(){
            addChild(Hero);
            Hero.x = start_x;
            Hero.y = start_y;
            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);
            }
            if(right){
                Hero.x_speed = walkspeed;
                setDirection(0);
            }
            if(up && Hero_col.can_jump){
                Hero.y_speed = -jumpspeed;
            }

            if(Hero.y_speed > max_speed){
                Hero.y_speed = max_speed;
            }

            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;
        }

感谢您的帮助。

【问题讨论】:

  • 地板平整吗?如果是平的,你可以使用stageHeighthero.y
  • 感谢您的回答,我现在使用if (Hero.y == 150){ Hero.y_speed = 0; },但现在它在地板上时我无法跳转

标签: actionscript-3 flash actionscript actionscript-2 stage


【解决方案1】:

您需要有几个布尔变量来检查英雄是否在地板上,以及何时应用重力。因此,如果他到达地板或特定区域,您必须将重力设置为 false,而不是像您在这里所做的那样每帧都更新它:Hero.y_speed += gravity;

如果你按下空格键,你需要让角色跳跃,然后你应用重力,通过将布尔值设置为 true,之后,当它撞到地板时,你应该禁用重力,然后再次启用跳跃。

void update(float delta) {
    if (grounded) { 
        // ground based movement and actions

        // check to see if the spacebar was pressed to jump
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) {
            ya = -40;   // go up 40 pixels per second
            grounded = false;
        }
    } else {
        // air based movement and actions

        // apply gravity
        ya += GRAVITY * delta;    
    }

    // movement and collision work after you've figured out the x and y acceleration of the player
    y += ya * delta;
}

【讨论】:

  • 谢谢你的回复,你能帮我吗?它应该如何正确?
  • 我试过了:private var gravity:Boolean = true; if(Hero.y_speed>0 && Hero.y>(stage.stageHeight-Hero.height-10)){ gravity = false;} 但它不起作用
  • 首先重力应该是假的。当你跳转它应该是假的,在特定时间之后,你启用它。为您的目的修改我的伪代码。
  • 感谢您的回答,但我收到此错误:1084: Syntax error: expecting rightparen before delta. 在此行:void update(float delta) {
  • 我也收到了这个错误:1083: Syntax error: else is unexpected.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 2011-11-04
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
相关资源
最近更新 更多