as3 hitTestObject 与 for 循环

发布于 2025-01-03 23:30:36 字数 633 浏览 0 评论 0原文

我正在构建一个吃豆人风格的游戏,我希望缩短我的代码。 该阶段包含 92 个实例,实例名称为 food1、food2 等。 当玩家(吃豆人)点击其中一个实例时,我想调用一个函数。

我开始这样写……它有效,但我不想重复这个 92 次!

if( player.hitTestObject(food1) ) {
    updateScore();
}

if( player.hitTestObject(food2) ) {
    updateScore();
}

现在我正在尝试一些类似的事情,但还没有成功。

function collectFood() {
    var i:Number;
    var pGroup:String

    for (i=0; i<92; i++) {
        pGroup= "food" + i;
        if( player.hitTestObject( MovieClip(pGroup) ) ) {
            pCount+= 1;
            MovieClip(pGroup).y=-300;
            updateScore();
        }
    }
}

感谢您的帮助!

I am building a pacman style game, and I am looking to shorten my code.
The stage contains 92 instances with instance names of food1, food2, etc.
When the player(pacman) hits one of the instances Im wanting to call a funcion.

I started writing it out like this... it works but I don't want to duplicate this 92 times!

if( player.hitTestObject(food1) ) {
    updateScore();
}

if( player.hitTestObject(food2) ) {
    updateScore();
}

Now I'm trying something along these lines, but with no success yet.

function collectFood() {
    var i:Number;
    var pGroup:String

    for (i=0; i<92; i++) {
        pGroup= "food" + i;
        if( player.hitTestObject( MovieClip(pGroup) ) ) {
            pCount+= 1;
            MovieClip(pGroup).y=-300;
            updateScore();
        }
    }
}

Thanks for any help!

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

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

发布评论

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

评论(1

淡淡離愁欲言轉身 2025-01-10 23:30:36

考虑将食物存储在数组中。

设置一个数组:

var food:Array = [];

将您的食物添加到此数组中(如果适用):

food.push(myFood);

然后您可以使用 foreach 来遍历此数组中的项目:

for each(var i:MovieClip in food)
{
    // do stuff with i
    // i represents an instance of your food

    if(player.hitTestObject(i))
    {
        pCount ++;
        pGroup.y -= 300;

        updateScore();
    }
}

如果您的食物已放在舞台上并且想要要将它们全部添加到此数组中,只需执行以下操作:

for(var i:int = 0; i<92; i++)
{
    food.push(MovieClip(this["food"] + i));
}

我强烈建议您查看 面向对象编程(OOP) 用于 ActionScript-3。使用 OOP,您将能够为您的 Food封装一块食物应该做的一切。

它还将允许您拥有更加清晰易读的代码,特别是在上面的 for every 循环中,它可能如下所示:

for each(var i:Food in food)
{
    // i is an instance of Food
}

作为美观的好处,使用像 FlashDevelop 将根据您添加到 Food 类的内容提供非常有用的工具提示,例如:

在此处输入图像描述

Look into storing your food in an Array.

Set up an Array:

var food:Array = [];

Add your food items into this array (wherever applicable):

food.push(myFood);

And then you can use for each to run through the items in this Array:

for each(var i:MovieClip in food)
{
    // do stuff with i
    // i represents an instance of your food

    if(player.hitTestObject(i))
    {
        pCount ++;
        pGroup.y -= 300;

        updateScore();
    }
}

If you have your food on the stage and want to add them all into this Array, just do this:

for(var i:int = 0; i<92; i++)
{
    food.push(MovieClip(this["food"] + i));
}

I strongly suggest that you look into Object-oriented programming (OOP) for ActionScript-3. Using OOP you'll be able to create a class for your Food and encapsulate everything that a piece of Food should do.

It will also allow you to have much cleaner and readable code, particularly in the above for each loop, which could look like this:

for each(var i:Food in food)
{
    // i is an instance of Food
}

As an aesthetic benefit, using an application like FlashDevelop will provide extremely helpful tooltips based on what you add to your Food class, eg:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文