[英]How can I detect touch in cocos2d?
我正在使用cocos2d為iPhone開發一款2D游戲。
我在游戲中使用了很多小精靈(圖像)。 我想觸摸兩個相似類型的精靈(圖像),然后兩個精靈(圖像)將被隱藏。
如何檢測特定精靈(圖像)中的觸摸?
更好的方法是實際使用sprite本身的邊界框(這是一個CGRect)。 在這個示例代碼中,我將所有精靈放在NSMutableArray中,並且我簡單地檢查精靈觸摸是否在邊界框中。 確保在init中打開觸摸檢測。 如果你注意到我也通過返回YES(如果我使用觸摸)或否(如果我沒有)接受/拒絕圖層上的觸摸
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [self convertTouchToNodeSpace: touch];
for (CCSprite *station in _objectList)
{
if (CGRectContainsPoint(station.boundingBox, location))
{
DLog(@"Found sprite");
return YES;
}
}
return NO;
}
按照喬納斯的指示,再添加一點......
- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touch = [touches anyObject];
CGPoint location = [[[Director sharedDirector] convertCoordinate: touch.location];
CGRect particularSpriteRect = CGMakeRect(particularSprite.position.x, particularSprite.position.y, particularSprite.contentSize.width, particularSprite.contentSize.height);
if(CGRectContainsPoint(particularSpriteRect, location)) {
// particularSprite touched
return kEventHandled;
}
}
您可能需要調整x / ya以適應Cocos中的“居中定位”
在包含您的精靈的圖層中,您需要說:
self.isTouchEnabled = YES;
然后你可以使用你在UIView中使用的相同事件,但它們的命名有點不同:
- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touch = [touches anyObject];
//in your touchesEnded event, you would want to see if you touched
//down and then up inside the same place, and do your logic there.
}
@david,你的代碼有一些關於cocos 0.7.3和2.2.1的拼寫錯誤,特別是CGRectMake而不是CGMakeRect和[touch location]現在是[touch locationInView:touch.view]。
這就是我做的:
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];
CGRect myRect = CGRectMake(sprite.position.x, sprite.position.y, sprite.contentSize.width, sprite.contentSize.height);
if(CGRectContainsPoint(myRect, location)) {
// particularSprite touched
return kEventHandled;
}
}
這是一個很好的教程,解釋了基本的觸摸系統http://ganbarugames.com/2010/12/detecting-touch-events-in-cocos2d-iphone/
首先,寫
self.isTouchEnabled = YES;
那么,你需要實現ccTouchesEnded,ccTouchesBegan等功能
根據我的理解,你希望能夠“匹配”兩個可以在屏幕上不同坐標上的精靈。
這樣做的方法.. :(我確定還有很多其他方法)
考慮有2個全局變量。
因此,每當觸摸觸摸精靈時,您都會使用多次提到的CGRectContainsPoint函數來查找已觸摸的精靈。 然后,您可以將該精靈的“標記”保存在其中一個全局變量中。
您在第二次觸摸時執行相同操作,然后比較2個全局變量。
如果你有問題,你應該能夠弄清楚其余的但是要評論。
這是它對我有用的方法......其中spriteSize顯然是精靈的大小......:P
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];
CGRect myRect = CGRectMake(sprite.position.x-spriteSize/2, sprite.position.y-spriteSize/2, spriteSize, spriteSize);
if(CGRectContainsPoint(myRect, location)) {
// particularSprite touched
return kEventHandled;
}
}
@Genericrich:CGRectContainsPoint在CocosLand中工作,因為上面有2行調用:
[[Director sharedDirector] convertCoordinate:]
Cocos2D對象將使用OpenGL坐標系,其中0,0是左下角,UIKit坐標(如觸摸發生的位置)左上角有0,0。 convertCoordinate:正在為您從UIKit翻轉到OpenGL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.