简体   繁体   English

Cocos2D-触摸了CCSprite之后弹出

[英]Cocos2D - CCSprite touched followed by popup

I've been stumped for days trying to get my sprite touch method under control. 试图控制我的精灵触摸方法已经困扰了我好几天。 My aim is to have another sprite pop up offset to a touched sprite. 我的目标是使另一个精灵弹出,使其与触摸的精灵偏移。

I currently have a half baked code for this process, in which I have experimented with many varieties of code, see links below, none of which has worked. 我目前对此过程有半熟的代码,其中我尝试了多种代码,请参见下面的链接,但没有一个起作用。 My questions: 我的问题:

  1. Getting CCTouchBegan to run the method towerPositionTapped once the touch hits a sprite on screen (the NSLog in CCTouchBegan is also not working). 一旦触摸碰到屏幕上的精灵,就使CCTouchBegan运行方法towerPositionTapped(CCTouchBegan中的NSLog也不起作用)。
  2. Find out how to get the location of the touched sprite so that it can be used in the towerPositionTapped method. 了解如何获取触摸的精灵的位置,以便可以在towerPositionTapped方法中使用它。

My touch specific code so far in the main layer: 到目前为止,我的触摸特定代码在主层中:

-(void) registerWithTouchDispatcher{
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 
    swallowsTouches:YES];
}


-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{

CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];


//_towersFromClass is an NSMutableArray with CCSprite objects
for(CCSprite *tb in _towersFromClass){
    if(CGRectContainsPoint(tb.boundingBox,location)){

        NSLog(@"sprite touched at %@", NSStringFromCGPoint(location));

        [self towerPositionTapped]

        return YES;
    }
}
return NO;
}

The NSLog check doesn't end up showing when I click on any of the sprites. 当我单击任何一个精灵时,NSLog检查不会最终显示。

For the towerPositionTapped method I am unsure how to capture the touched location (or record the touched sprite) in order to position the new sprite. 对于towerPositionTapped方法,我不确定如何捕获触摸的位置(或记录触摸的精灵)以定位新的精灵。

towerPositionTapped.m towerPositionTapped.m

-(void)towerPositionTapped{

CCMenuItem *towerOption1 = [CCMenuItemImage itemWithNormalImage:@"tower.png" selectedImage:@"tower.png"];
towerOption1.position = /* place touched sprite location here, with an offset*/;

CCMenu *_towerOptionsMenu = [CCMenu menuWithItems: nil];
_towerOptionsMenu.position = CGPointZero;
[self addChild:_towerOptionsMenu z:5];

} }

Any constructive criticism is appreciated. 任何建设性的批评表示赞赏。 Thank you for your time. 感谢您的时间。

A few of the sites I have been testing: 我正在测试的一些站点:

Sprite Offset 精灵偏移

Touch 1 触摸1

Touch 2 触摸2

If you CCTouchBegin in not working ( not inside for loop but overall) Then may be you have to try this line if you havnt EnableTouch 如果您的CCTouchBegin不能正常工作(不在for循环内部,而是整体),那么如果您启用了EnableTouch,则可能必须尝试此行

self.isTouchEnabled = YES;

I am using this line rather than registerWithTouchDispatcher. 我使用的是这一行,而不是registerWithTouchDispatcher。

It's taken me a few days to solve this, none the less I have managed to come up with a solution. 解决这个问题花了我几天的时间,尽管如此,我还是设法提出了一个解决方案。

I've basically used the same code as in Ray Wenderlich's Tower Defence Tutorial ( RWTDTutorial ). 我基本上使用了与雷·温德利希(Ray Wenderlich)的塔防教程( RWTDTutorial )中相同的代码。 Using the CCTouchesBegan method. 使用CCTouchesBegan方法。

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

for(UITouch *touch in touches){
    CGPoint location = [touch locationInView:[touch view]];

    location = [[CCDirector sharedDirector] convertToGL:location];

    for(CCSprite *tb in _towersFromClass){

        if(CGRectContainsPoint([tb boundingBox],location)){

            selectedTowerPosition = tb;
            [self towerPositionTapped];

        }
    }
}
}

I created another method which contains the CCMenu items I wanted to pop up after I selected one of the CCSprites in my _towersFromClass array, and added that under the 'if' statement of the CCTouchesBegan method. 我创建了另一个方法,该方法包含要在_towersFromClass数组中选择一个CCSprite之后要弹出的CCMenu项,并将其添加到CCTouchesBegan方法的'if'语句下。

In order to get the correct location for the menu, I created a CCSprite property '_selectedTowerPosition' and made it = the for 'tb' CCSprite. 为了获得菜单的正确位置,我创建了一个CCSprite属性'_selectedTowerPosition',并使其= for'tb'CCSprite。

Back in the CCMenu method I used the ccpAdd code, as seen below, to position the menu. 回到CCMenu方法中,我使用了ccpAdd代码(如下所示)来放置菜单。

CCMenuItem *towerOption1 = [CCMenuItemImage itemWithNormalImage:@"tower.png" selectedImage:@"tower.png" target:self selector:@selector( towerSelectOption)];
towerOption1.position = ccpAdd(_selectedTowerPosition.position,ccp(40,0));

I also gave the menu item a slight offset. 我还为菜单项提供了一些补偿。

It's taken me awhile but I've learned a lot from this experience. 我花了一段时间,但我从这次经历中学到了很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM