Cocos2d iphone 如何检查精灵是否按特殊顺序命中
我希望读者按顺序点击屏幕上的3个或8个精灵: 先触摸鸡蛋,然后触摸糖,最后触摸柠檬(如果操作正确,会出现一个金杯)Cocos2d iphone 如何检查精灵是否按特殊顺序命中,cocos2d-iphone,Cocos2d Iphone,我希望读者按顺序点击屏幕上的3个或8个精灵: 先触摸鸡蛋,然后触摸糖,最后触摸柠檬(如果操作正确,会出现一个金杯) 鸡蛋泥 糖雪碧 柠檬汁 我拥有的功能可以使用,但我想知道是否有更简单的方法来维护和扩展屏幕上的其他精灵(我有8个精灵,希望能产生不同的“回复”) cctouchs我开始检测精灵的触摸 if(CGRectContainsPoint(EggsSprite.boundingBox, location)) { EggsSprite_is_hit = YES; NSLo
- 鸡蛋泥
- 糖雪碧
- 柠檬汁
if(CGRectContainsPoint(EggsSprite.boundingBox, location))
{
EggsSprite_is_hit = YES;
NSLog(@"EggsSprite_is_hit = YES");
}
if(CGRectContainsPoint(SugarSprite.boundingBox, location))
{
SugarSprite_is_hit = YES;
NSLog(@"RunCheckSugar");
[self CheckSugar];
}
if(CGRectContainsPoint(LemonSprite.boundingBox, location))
{
NSLog(@"RunCheckLemon");
LemonSprite_is_hit = YES;
[self CheckLemon];
}
这将运行
-(void)CheckLemon
{
NSLog(@"LemonSprite is hit");
if(MeringueUnlocked == YES)
{
NSLog(@"You made a merangue");
Award.opacity=255;
}
else if(MeringueUnlocked == NO)
{
NSLog(@"Incorrect order");
EggsSprite_is_hit = NO;
LemonSprite_is_hit = NO;
SugarSprite_is_hit = NO;
MeringueUnlocked = NO;
}
}
-(void)CheckSugar
{
if(SugarSprite_is_hit == YES && EggsSprite_is_hit== YES)
{
NSLog(@"SugarSprite is hit");
NSLog(@"And Egg Sprite is hit");
SugarSprite_is_hit = NO;
MeringueUnlocked = YES;
}
else if(SugarSprite_is_hit == YES && EggsSprite_is_hit== YES)
{
NSLog(@"SugarSprite not hit ressetting all");
EggsSprite_is_hit = NO;
LemonSprite_is_hit = NO;
SugarSprite_is_hit = NO;
MeringueUnlocked = NO;
}
}
它似乎工作正常,但它将是可怕的扩展,我似乎找不到任何触摸精灵的例子,以便任何psudo代码的想法将是受欢迎的:)因为它的方法,我更坚持,因为我是新的编码
谢谢:)
N创建精灵时,为其“标记”属性指定命中它们的顺序:
EggsSprite.tag = 1;
SugarSprite.tag = 2;
LemonSprite.tag = 3;
然后创建一个实例变量来存储上次精灵命中的索引:
int _lastSpriteHitIndex;
对于序列中最后一个精灵的索引:
int _finalSpriteIndex;
在init
中的某个位置设置其值(或使用任何方法创建图层):
然后在触摸处理器中:
// find sprite which was touched
// compare its tag with tag of last touched sprite
if (_lastSpriteHitIndex == touchedSprite.tag - 1)
{
// if it is next sprite in our planned order of sprites,
// store its tag as _lastSpriteHitIndex
_lastSpriteHitIndex = touchedSprite.tag;
}
else
{
// if it's wrong sprite, reset sequence
_lastSpriteHitIndex = 0;
}
if (_lastSpriteHitIndex == _finalSpriteIndex)
{
// Congrats! You hit sprites in correct order!
}
基本上,它是一个有限状态机,按正确的顺序敲击精灵会将机器推进到下一个状态,而敲击错误的精灵会将机器重置到初始状态<代码>\u lastSpriteHitIndex表示当前状态,\u finalSpriteIndex
表示最终状态
如果您不想在错误的精灵命中时重置为初始状态,只需删除
else
子句-如果没有它,机器在命中错误的精灵时将无法前进。非常感谢您!我还没有进行测试,但我认为这会很好,所以我接受了答案。最后,我制作了一个数组来存储精灵,然后使用您的代码来排序触摸顺序,感谢您分享了这样一个简单而酷的解决方案:)
// find sprite which was touched
// compare its tag with tag of last touched sprite
if (_lastSpriteHitIndex == touchedSprite.tag - 1)
{
// if it is next sprite in our planned order of sprites,
// store its tag as _lastSpriteHitIndex
_lastSpriteHitIndex = touchedSprite.tag;
}
else
{
// if it's wrong sprite, reset sequence
_lastSpriteHitIndex = 0;
}
if (_lastSpriteHitIndex == _finalSpriteIndex)
{
// Congrats! You hit sprites in correct order!
}