【问题标题】:Problems in my AS2 Game我的 AS2 游戏中的问题
【发布时间】:2011-06-18 00:17:01
【问题描述】:

大家好,我正在尝试制作一个类似于下面这个游戏的 2D 平台风格游戏:

http://www.gameshed.com/Puzzle-Games/Blockdude/play.html

我已经完成了大部分的图形、区域和碰撞,但我们的角色仍然无法携带东西。我对使用什么代码让我的角色可以携带积木感到困惑。我需要关于如何让我们的角色携带他面前的积木的帮助,前提是积木上面没有任何东西。这已经让我困惑了一个星期,任何帮助将不胜感激。 :D

【问题讨论】:

    标签: flash-cs4 actionscript-2


    【解决方案1】:

    我这样做的方法是为他将拾取的每个块设计一个单独的命中测试,然后为命中测试编写代码以在他携带一个块的精灵时间轴内播放一帧,并播放一帧在要拾取的块的时间轴内不再静止(消失?)。

    祝你好运,如果您对我所说的内容感到困惑,请多问一些问题,如果可以,我会尽力帮助您。

    【讨论】:

    • OP 不会问,因为这是一个已有 4 年历史的问题,并且 OP 不再处于活动状态,而不是有人询问并稍后在答案中添加更多内容。添加更多详细信息并使其成为与其他答案一样的完整答案。我会赞成的。
    【解决方案2】:

    我怀念我的第一个 AS2 游戏。正如我将解释的那样,最好的方法可能是面向对象的方法。

    在 AS2 中,有一个自动内置到对象中的 hittest 方法。这里有一个很好的关于 Kirupa 的教程:

    http://www.kirupa.com/developer/actionscript/hittest.htm

    还有

    http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001314.html

    首先,您需要使用 Box 类生成您的盒子。您的课程需要如下所示:

    //Box.as pseudo-code
    class Box {
        var x_pos:Number;
        var y_pos:Number;
        var attachedToPlayer:Boolean;
    
        function Box(_x:Number, _y:Number) {
            this.x_pos = _x;
            this.y_pos = _y;    
        }
    
        //other code here
    
    }
    

    请参阅本教程,了解如何将类附加到库中的对象:

    http://www.articlesbase.com/videos/5min/86620312

    要创建一个新盒子,你可以使用类似的东西 盒子1 =新盒子(100,200); // 在位置 100x,200y 处创建一个框

    但是,您还需要将要拾取的块存储到某种数组中,以便循环遍历它们。见http://www.tech-recipes.com/rx/1383/flash-actionscript-create-an-array-of-objects-from-a-unique-class/

    例子:

    //在你的主方法顶部附近的某个地方,或者你的主游戏循环从哪里运行 - 注意 Box.as 需要在同一个文件夹中 导入框;

    //...then, somewhere before your game loop
    
    //create an array to hold the objects
    var boxArray:Array = new Array();
    
        //create loop with i as the counter
        for (var i=0; i<4; i++)
        {
            var _x:Number = 100 + i;
            var _y:Number = 100 + i;
            //create Box object
            var box:Box = new Box();
    
            //assign text to the first variable.
            //push the object into the array
            boxArray.push(box);
        }
    

    同样,您需要为您的玩家创建一个类,并在游戏开始时创建一个新的 Player 对象,例如

    var player = new Player(0,0);
    

    然后,您可以针对主游戏循环(即更新玩家位置和其他游戏属性的循环)数组中的块为您的玩家运行一个 hittest 方法。可能有更有效的方法可以做到这一点,例如只循环当前在屏幕上的块。

    创建数组后,使用 foreach 循环在游戏的主循环中对玩家运行命中测试,例如

    //assuming you have an array called 'boxArray' and player object called 'player'
    for(var box in boxArray){
        if (player.hittest(box)) {
            player.attachObjectMethod(box);
        }
    }
    

    这基本上是“对于我们输入数组的每个盒子,检查玩家是否正在触摸盒子。如果盒子正在触摸,则使用盒子作为播放器类中方法的参数(我随意称为attachObjectMethod)”。

    在 attachObjectMethod 中,您可以定义某种行为来将盒子附加到播放器上。例如,您可以为 box 类中的框的 x 和 y 位置创建一个 get 和 set 方法,以及一个名为 attachToPlayer 之类的有用的布尔值。调用 attachObjectMethod 时,它将设置框的布尔值,例如在 Player 类中

    //include Box.as at the top of the file
    import Box;
    
    //other methods, e.g. constructor
    
    //somewhere is the Player.as class/file
    public function attachObjectMethod (box:Box) {
        box.setattachedToPlayer(true);
        //you could also update fields on the player, but for now this is all we need
    }
    

    现在,玩家碰撞过的盒子的 attachToPlayer 布尔值为真。回到我们的游戏循环,然后我们将修改我们的循环以更新盒子的位置:

    //assuming you have an array called 'boxArray' and player object called 'player'
    for(var box in boxArray){
        if (player.hittest(box)) {
            player.attachObjectMethod(box);
        }
        box.updatePosition(player.get_Xpos, player.get_Ypos);
    }
    

    在我们的 Box 类中,我们现在需要定义“updatePosition”:

    //Box.as pseudo-code
    class Box {
        var x_pos:Number;
        var y_pos:Number;
        var attachedToPlayer:Boolean;
    
        function Box(box_x:Number, box_y:Number) {
            this.x_pos = box_x;
            this.y_pos = box_y;     
        }
    
        public function updatePosition(_x:Number, _y:Number) {
            if (this.attachedToPlayer) {
                this.x_pos = _x;
                this.y_pos = _y;
            }
        }
    
        //other code here
    
    }
    

    如您所见,我们可以传递玩家的位置,如果已设置 attachToPlayer 布尔值,则更新框的位置。最后,我们给盒子添加一个move方法:

    public function move() {
        if (this.attachedToPlayer) {
            this._x = x_pos;
            this._y = y_pos;
        }
    }
    

    更新位置示例: http://www.swinburne.edu.au/design/tutorials/P-flash/T-How-to-smoothly-slide-objects-around-in-Flash/ID-17/

    最后,我们需要在游戏循环中调用 move 方法:

    //assuming you have an array called 'boxArray' and player object called 'player'
    for(var box in boxArray){
        if (player.hittest(box)) {
            player.attachObjectMethod(box);
        }
        box.updatePosition(player.get_Xpos, player.get_Ypos);
        box.move();
    }
    

    你还指定了只有在上面没有任何东西的情况下,方块才应该随着玩家移动。当您调用您的 attachToPlayer 方法时,您还需要在盒子和可能位于盒子顶部的对象之间运行一个 foreach 循环。您现在应该从上面的代码中对如何做到这一点有了一个大致的了解。

    我很欣赏这是一个相当冗长的答案,而且我还没有机会测试所有代码(事实上我很肯定我在某个地方犯了一个错误) - 不要犹豫提出问题。我的另一个建议是彻底理解这些概念,然后一次一点地编写自己的代码。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-23
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      相关资源
      最近更新 更多