iOS SpriteKit冲突检测:重置NSMutable对象数组的位置 [英] IOS SpriteKit Colision Detection: Resetting Position of NSMutable array of Objects

查看:107
本文介绍了iOS SpriteKit冲突检测:重置NSMutable对象数组的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个在屏幕左右壁之间反弹直到到达底部的物体的游戏。

I'm attempting to make a game with objects that bounce between the left and right walls of the screen until they reach the bottom.

目前,我的工作didBeginContact方法如下所示:

Currently, my working didBeginContact method looks like this:

- (void)didBeginContact:(SKPhysicsContact *)contact {
    uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

    if (collision == (crabCategory | leftWallCategory)) {
    // figure out which crab in the array made the contact...
        for(HHCrab *object in crabs) {
            if(([object.name isEqualToString:contact.bodyB.node.name]) || ([object.name isEqualToString:contact.bodyA.node.name])) {
                [object stop];
                [object moveRight];
            }
        } 
    } else if (collision == (crabCategory | rightWallCategory)) {
    // figure out which crab in the array made the contact...
        for(HHCrab *object in crabs) {
            if(([object.name isEqualToString:contact.bodyB.node.name]) || ([object.name isEqualToString:contact.bodyA.node.name])) {
                [object stop];
                [object moveLeft];
            }
        }
    }

当我尝试添加附加语句:

When I attempt to add an additional statement:

else if (collision == (crabCategory | bottomWallCategory)) {
    // figure out which crab in the array made the contact...
    for(HHCrab *object in crabs) {
        if(([object.name isEqualToString:contact.bodyB.node.name]) || ([object.name isEqualToString:contact.bodyA.node.name])) {
            [object stop];
            [object resetPosition];
            [object moveLeft];
        }
    }
}

重置螃蟹的位置它们到达屏幕的底部,螃蟹将不会移动。在之前的问题中,我意识到可能是由于物理模拟已覆盖绘制螃蟹的事实。我们通过创建一个布尔值解决了这个问题,该布尔值将在螃蟹与底壁相交时打开和关闭,然后在didSimulatePhysics方法中将螃蟹重新放置为:

to reset the crabs position when they reach the bottom of the screen, the crabs will not move. In a previous question I was made aware that this may be due to the fact that drawing the crab was being overwritten by the physics simulation. We solved the issue by creating a Boolean which would be toggled on and off when the crab intersects the bottom wall, and then repositions the crab in the didSimulatePhysics method as such:

-(void)didSimulatePhysics{
    if(self.shouldResetPosition){
       self.player.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMaxY(self.frame)- _player.size.height-30);
        self.shouldResetPosition = NO;
    }
}  

虽然这只适用于一只螃蟹,但我现在使用每个具有唯一名称的NSMutable螃蟹数组,事实并非如此。我尝试将布尔值移动到Crab类,因此每个螃蟹都具有该属性,但这不能解决问题。

Although this works with one crab, now that I am using an NSMutable Array of Crabs each with a unique name, it does not. I tried moving the Boolean value to the Crab class, so each crab has that property but this did not solve the problem.

推荐答案

您需要对OOD(面向对象开发)概念进行一些研究。我建议买一本关于Objective-C编程的书,教那些概念。

You need to do some study on OOD (Object Oriented Development) concepts. I'd suggest buying a book on Objective-C programming that teaches those concepts.

您在创建自定义Crab对象的正确轨道上。使它成为UIView的子类(或自定义基类的子类,但暂时将其忽略)。然后,您可以创建一个竞技场或场景对象,将其用作螃蟹对象的容器。它会有一个可变的数组,可以容纳您的螃蟹对象数组。

You're on the right track creating a custom Crab object. Make it a subclass of UIView (or of a custom base class, but ignore that for a moment). Then you might create an "Arena" or "Scene" object that would serve as the container for your crab objects. It would have a mutable array that would hold your array of crab objects.

您可能想对Arena对象进行泛化,以便管理一系列知道如何操作的抽象视图对象绘制自己,并具有一些常见的动画设置方法。为所有自定义视图对象创建一个基类,然后使crab对象成为此自定义视图对象的子类。这样,如果您以后要添加Fish对象,Snail对象,甚至Boat对象,Net对象或其他任何对象,则无需重新执行任何操作即可。

You might want to generalize your Arena object so it manages an array of abstract view objects that know how to draw themselves and have some common animation setup methods. Create a base class for all your custom view objects, and then make the crab object a subclass of this custom view object. That way, if you later want to add Fish objects, or Snail objects, or even Boat objects or Net objects or whatever, you can do so without having to re-do everything.

您可以使用UIView动画或Core Animation来使螃蟹对象自己动画,也可以使用计时器和基于帧的动画。使用内置的动画方法之一,您将获得更流畅的动画和更好的性能。

You could use UIView animation or Core Animation to make your crab objects animate by themselves, or you could use timers and frame-based animation. You'll get smoother animation and better performance using one of the built-in animation approaches.

您的控制代码将产生多个自定义视图对象(例如10个Crabs,12条Fish ,船,渔民和网),将其配置为位置和速度/行为,然后将其添加到舞台对象。 (竞技场可能会将它们添加到数组中以便跟踪它们。)然后,竞技场对象将使用基类的共享方法来管理这些对象。

Your control code would spawn multiple custom view objects (e.g. 10 Crabs, 12 Fish, a Boat, a Fisherman, and a Net), configure them for location and speed/behavior, and add them to the arena object. (The arena would likely add them to an array in order to keep track of them.) The arena object would then manage those objects using the shared methods of the base class.

这篇关于iOS SpriteKit冲突检测:重置NSMutable对象数组的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆