gpt4 book ai didi

ios - 如何在一次触摸后禁用 CGRect/Sprite 上的触摸

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:20 25 4
gpt4 key购买 nike

如何在 CCRect/sprite 被触摸后禁用它的触摸?

我在初始化方法中设置了 Sprite :

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"testAtlas_default.plist"];
sceneSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"testAtlas_default.png"];

[self addChild:sceneSpriteBatchNode z:0];

dinosaur1_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur1-c.png"];
[sceneSpriteBatchNode addChild:dinosaur1_c];

[dinosaur1_c setPosition:CGPointMake(245.0, winSize.height - 174.0)];

然后我使用 Sprite 的位置和大小作为其参数创建一个 CGRect:

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];

dinosaur1 = CGRectMake(dinosaur1_c.position.x - (dinosaur1_c.contentSize.width / 2), dinosaur1_c.position.y - (dinosaur1_c.contentSize.height / 2), dinosaur1_c.contentSize.width, dinosaur1_c.contentSize.height);

if( CGRectContainsPoint(dinosaur1, touchLocation) )
{
CCLOG(@"Tapped Dinosaur1_c!");
PLAYSOUNDEFFECT(PUZZLE_SKULL);

// Code to disable touches??
// Tried to resize CGRect in here to (0, 0, 1, 1) to get it away from the original sprite, but did not work. Still was able to tap on CGRect.
// Tried [[CCTouchDispatcher sharedDispatcher] setDispatchEvents:NO];, but disables ALL the sprites instead of just this one.

}
}

我能够成功地点击 sprite 使其播放声音,但是我就是不知道如何在 CGRect 被触摸后禁用它。我尝试了上面代码中评论的不同方法。任何想法或提示都将不胜感激!

最佳答案

这对你也有帮助

- (void)selectSpriteForTouch:(CGPoint)touchLocation {
for (CCSprite *sprite in _projectiles) {

if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {

NSLog(@"sprite was touched");


[sprite.parent removeChild:sprite cleanup:YES];

[self removeChild:sprite.parent cleanup:YES];

}
} }

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
[self selectSpriteForTouch:touchLocation];
NSLog(@"touch was _");
return TRUE; }

关于ios - 如何在一次触摸后禁用 CGRect/Sprite 上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9387592/

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