AS3 确定什么对象重叠它?

发布于 2024-12-13 13:58:52 字数 109 浏览 0 评论 0原文

我目前正在用 as3 构建一个游戏;我现在遇到的问题是,当我掷虚拟骰子时,玩家(标记)在棋盘上移动,但我需要知道的是:有没有办法找到玩家着陆的对象(盒子)的实例名称在?

抱歉我的英语不好。

I'm current building a game in as3; the proplem I have right now is when I roll the virtual dice, the player(marker) moves accross the board but what I need to know is: is there a way to find the instance name of the object(box) that the player lands on?

And Sorry my english isn't good.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红焚 2024-12-20 13:58:52

这在很大程度上取决于您的电路板的布局方式。一种方法是将玩家可以降落的所有对象放入一个数组中,然后检查玩家的 x 和 y 坐标以查看它们是否落入每个对象的框内。

例如:

var boardObjects:Array; // This would contain references to all the objects the 
    // player object might land on. Initialize it, then use boardObjects.add(object) 
    // on each one until they're all in the array.

// once the player has moved:
for(var i:int = 0; i < boardObjects.size; i++) {
    var obj:* = boardObjects[i];
    if (player.x >= obj.x && player.x <= obj.x + obj.width) {
        if (player.y >= obj.y && player.y <= obj.y + obj.height) {
            // If these if statements are all true, the Player's top-left corner
            // is inside the object's bounding box. If this is a function,
            // here is a good spot to put a return statement.
        }
    }
}

您可能希望根据玩家的中间而不是左上角来计算它,在这种情况下,只需将玩家宽度的一半添加到其 x 位置,将其高度的一半添加到 y 位置。

It depends a lot on how your board is laid out. One way is to put all of the objects your player can land on into an array, then check the player's x and y coordinates to see if they fall inside of each object's box.

For example:

var boardObjects:Array; // This would contain references to all the objects the 
    // player object might land on. Initialize it, then use boardObjects.add(object) 
    // on each one until they're all in the array.

// once the player has moved:
for(var i:int = 0; i < boardObjects.size; i++) {
    var obj:* = boardObjects[i];
    if (player.x >= obj.x && player.x <= obj.x + obj.width) {
        if (player.y >= obj.y && player.y <= obj.y + obj.height) {
            // If these if statements are all true, the Player's top-left corner
            // is inside the object's bounding box. If this is a function,
            // here is a good spot to put a return statement.
        }
    }
}

You may want to calculate it based on the middle of the player rather than their top-left corner, in which case just add half the player's width to their x position and half their height to their y position.

飘过的浮云 2024-12-20 13:58:52

为了性能(并避免不必要的代码),如果它是基于图块/骰子,为什么不做这样的事情

private function rollDice(){
    var results:Array = [Math.ceil(Math.random() * 6), Math.ceil(Math.random() * 6)] //Accurately simulates two 6 sided dice
    dice1.rollAnimation(results[0]);
    dice2.rollAnimation(results[1]);
    player.position += results[0] + results[1];
}

板将是一个数组,并且在播放器中您可以使用 getters/setters 来“包装”板,如下所示

private var _position:int = 0;
public function get position():int{
    return _position;
}
public function set position(value:int){
    _position = value;
    while(_position > GameBoard.TILES){
        _position -= GameBoard.TILES;
    }
    x = //Whatever you determine the positioning of the player..
}

For performance (and avoiding unnecessary code), if it's tile based / dice why not do something like this

private function rollDice(){
    var results:Array = [Math.ceil(Math.random() * 6), Math.ceil(Math.random() * 6)] //Accurately simulates two 6 sided dice
    dice1.rollAnimation(results[0]);
    dice2.rollAnimation(results[1]);
    player.position += results[0] + results[1];
}

The board would be an array, and in Player you can use getters/setters to 'wrap' the board like this

private var _position:int = 0;
public function get position():int{
    return _position;
}
public function set position(value:int){
    _position = value;
    while(_position > GameBoard.TILES){
        _position -= GameBoard.TILES;
    }
    x = //Whatever you determine the positioning of the player..
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文