gpt4 book ai didi

ios - CGRect 和触摸

转载 作者:行者123 更新时间:2023-11-28 20:33:35 24 4
gpt4 key购买 nike

在这个 cocos2d 应用程序中,当我按下 ccsprite 时,nslog 没有触发。有人可以帮助我吗?

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
for (CCSprite *target in _targets) {
CGRect targetRect = CGRectMake(target.position.x - (target.contentSize.width/2),
target.position.y - (target.contentSize.height/2),
27,
40);


CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
if (CGRectContainsPoint(targetRect, touchLocation)) {
NSLog(@"Moo cheese!");
}
}
return YES;
}

最佳答案

首先确保将用于触摸的 Sprite 注册到 onEnter 方法中,例如:

- (void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:defaultTouchPriority_ swallowsTouches:YES];
[super onEnter];
}

这将使您的 sprite 可触摸,因此当用户按下它时将事件触发到 sprite。然后重构您的代码以使其更具可读性并测试类似的内容:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

NSArray *targetsToDelete = [self touchedSpritesAtLocation:touchLocation];

// Put your code here
// ...

return YES;
}

- (NSArray *)touchedSpritesAtLocation:(CGPoint)location
{
NSMutableArray *touchedSprites = [[NSMutableArray alloc] init];

for (CCSprite *target in _targets)
if (CGRectContainsPoint(target.boundingBox, location))
[touchedSprites addObject:target];

return [touchedSprites autorelease];
}

它应该返回已被触及的目标。

关于ios - CGRect 和触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11376258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com