从图层中删除精灵而不是用它删除矩形(Cocos2d)?
我正在 Cocos2d 中制作游戏。到目前为止,一切都还好。我使用了 Ray Wenderlich 的 使碰撞检测发挥作用的教程。它有效,但每当“敌人”在子弹被删除的地方产生(因为被删除的子弹击中目标,因此被删除),敌人也会被自动删除。我认为这是因为它没有删除为精灵声明的矩形。请注意,即使子弹被删除,它也可以穿过多个敌人。任何帮助表示赞赏。谢谢!
编辑: 我发现问题出在哪里了。我在schedule:@selector方法中设置了拍摄方法,没有设定间隔。这意味着它能够以 60 fps 的速度快速发射子弹。所以我一点击就得到了两颗子弹。他们靠得很近,我花了好一会儿才注意到。我不会再犯这样的错误了!!!
I am making a game in Cocos2d. Everything is okay, so far. I used Ray Wenderlich's tutorial to get collision detection to work. It works, but whenever an 'enemy' spawns where a bullet was deleted (because the bullet that was deleted hit a target, therefore, was deleted), the enemy is automatically deleted, too. I think it's because it doesn't remove the rect that was declared for the sprite. Note, it also can go through more than one enemy, even though the bullet is deleted. Any help is appreciated. Thanks!
EDIT:
I found out what the problem was. I had the shoot method set in a schedule:@selector method, with no set interval. That meant that it would fire bullets 60fps fast. So I was getting TWO bullets with ONE click. They were so close together, that it took me a while to notice it. I won't make that mistake again!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用以下代码吗? (来自 如何制作一个简单的iPhone 游戏 Cocos2D 教程)
听起来子弹已从图层中删除,但并未从 _projectiles 数组中删除。
您确定这段代码有效吗?
Are you using the following code? (from How To Make A Simple iPhone Game with Cocos2D Tutorial)
It sounds that the bullet is removed from the layer, but it is not removed from _projectiles array.
Are you sure that this code works?
矩形不是与项目符号分开的实体。矩形是与项目符号关联的属性。一旦您的“项目符号被删除”,该矩形将不再有效。
您应该查看的是碰撞检查代码。
您可能想用这样的条件包围您的子弹碰撞检查代码:
由于您还没有发布代码,所以我只能在此处发布伪代码。也许如果您发布碰撞检查代码,我可以向您展示更详细的信息。
Rect is not a seperate entity from your bullet. Rect is the property associated with the bullet. As soon as your "bullet is deleted" the rect will no longer be valid.
What you should be looking at is your collision checking code.
You probably want to surround your bullet collision check code with a condition like:
Since you haven't posted code I could only post pseudo code here. Maybe if you post your collision checking code I could show you in more detail.