AndEngine - 有时精灵会留在屏幕上

发布于 2024-11-16 02:41:17 字数 981 浏览 3 评论 0原文

我正在尝试使用 AndEngine 创建 Android 游戏,并取得了一些成功。我正在尝试制作一个目标点击克隆,它基本上涉及点击屏幕上的许多不同目标来删除它们(有点像打地鼠)。

它与一个目标完美配合,我可以很容易地点击它来将其删除。问题是,当屏幕上有多个目标时,它们不会总是消失,但会添加分数以及击中一个目标时应该发生的所有其他事情。

我正在以(据我所知)正确的方式删除精灵,即在 runOnUpdateThread(...) 块内执行此操作。

Game.runOnUpdateThread(new Runnable() {
     @Override
     public void run() {

        // Loop through all targets and check validity
        for (Iterator<Target> i = Game.this.mTargets.iterator(); i.hasNext();) {

            Target t = i.next();  // Target extends Sprite

            // If the target isn't valid, remove it from the scene and ArrayList
            if (!t.isValid()) { 
                scene.unregisterTouchArea(t);
                scene.detachChild(t);
                Game.this.mTarget.remove(t);
            }
        }
}

抱歉,这有点简短,但因为我不确定问题出在哪里,所以我不知道要提供什么代码。我目前无法在真实设备上测试它,但想知道这是否可能只是与模拟器有关,因为据我所知代码是正确的并且我已经尝试了很多事情。如果您需要任何帮助,请告诉我!

谢谢

I'm attempting to create an Android game using AndEngine and have been having some successes. I'm trying to make a Target Tap clone which basically involves tapping a number of different targets on the screen to remove them (a bit like whack-a-mole).

It works perfectly with one target and I can quite easily tap on it to remove it. The problem is that when there is more than one target on the screen they don't always disappear but adding points and everything else that is supposed to happen when you hit one works.

I am removing the sprites in (as far as I know) the correct way which is to do it inside the runOnUpdateThread(...) block.

Game.runOnUpdateThread(new Runnable() {
     @Override
     public void run() {

        // Loop through all targets and check validity
        for (Iterator<Target> i = Game.this.mTargets.iterator(); i.hasNext();) {

            Target t = i.next();  // Target extends Sprite

            // If the target isn't valid, remove it from the scene and ArrayList
            if (!t.isValid()) { 
                scene.unregisterTouchArea(t);
                scene.detachChild(t);
                Game.this.mTarget.remove(t);
            }
        }
}

Sorry this is a little brief but because I'm unsure about where the problem lies I don't know what code to supply. I'm currently unable to test it on a real device but was wondering if this is could simply be something to do with the emulator because as far as I can tell the code is correct and I've tried so many things. If you need any help helping me, just let me know!

Thanks

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

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

发布评论

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

评论(1

面犯桃花 2024-11-23 02:41:17

当您删除一个 ArrayList 时,看起来您正在跳过 ArrayList。假设你的目标是(5),但结果无效。然后,它从列表中删除第 5 个元素,并将所有元素从那里向下移动一个,因此旧的第 6 个元素现在是第 5 个元素。然后,当您循环返回时,您会在 new 第 5 个元素后面点击 next()

通常,在这种情况下我要做的是:

(a)向后运行列表,或者

(b)如果删除一个布尔值,则将布尔值设置为 true,并在下一次迭代中执行 next() 函数之前检查它。或者,更有可能..

(c) 不要使用迭代器,而是使用 get() 函数,即

    for (int i=0;i<Game.this.mTarget.size();i++) {

        Target t = Game.this.mTarget.get(i);  // Target extends Sprite

        // If the target isn't valid, remove it from the scene and ArrayList
        if (!t.isValid()) { 
            scene.unregisterTouchArea(t);
            scene.detachChild(t);
            Game.this.mTarget.remove(t);
            // Decrease i to keep in sync with the new list
            i--;
        }
    }

It looks like you're skipping in the ArrayList when you remove one. Say you're on targets(5) and it comes up invalid. It then removes the 5th element from the list and shifts everything from there on down one, so the old 6th is now 5th. Then when you loop back through, you hit next() right past the new 5th element.

Normally what I do in this case is either:

(a) Run through the list backwards, or

(b) set a boolean to true if I remove one, and check it before performing the next() function in the next iteration. Or, more likely..

(c) don't use iterators, and instead use the get() function, ie

    for (int i=0;i<Game.this.mTarget.size();i++) {

        Target t = Game.this.mTarget.get(i);  // Target extends Sprite

        // If the target isn't valid, remove it from the scene and ArrayList
        if (!t.isValid()) { 
            scene.unregisterTouchArea(t);
            scene.detachChild(t);
            Game.this.mTarget.remove(t);
            // Decrease i to keep in sync with the new list
            i--;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文