cocoa-touch - 获取 Sprite 在数组 cocos2d 中的位置

标签 cocoa-touch cocos2d-iphone cocos2d-android cocos2d-x

我需要能够触摸数组中的特定移动 Sprite 并对其执行操作。但是,当我执行 MoveTo 操作时, Sprite 位置不会更新。救命!

数组:

int numbreds = 7;

redBirds = [[CCArray alloc] initWithCapacity: numbreds];

for( int i = 1; i<=numbreds; i++){

    int xvalue = ((-50*i) + 320);
    int yvalue= 160;


    if (i==4)
    { 
        CCSprite *parrot = [CCSprite spriteWithFile:@"taco.png"];

        [birdLayer addChild:parrot];
        [self movement]; //the action that moves the array horizontally
        parrot.position = ccp(xvalue,yvalue);
        parrot.tag=100;

触摸

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

{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];


CCSprite *mark = (CCSprite *)[birdLayer getChildByTag:100];

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

    CCLOG(@"YAY!");
}

问题在于 CCSprite 的位置实际上并未更新或移动。耶!仅在 Sprite 的原点位置生成。

最佳答案

试试这个:

CCSprite *temp = [CCSprite spriteWithFile:@"taco.png"];

temp = [birdLayer getChildByTag:100];

if (temp.position.x == location.x) {

    // do stuff...
}

关于cocoa-touch - 获取 Sprite 在数组 cocos2d 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11279429/

相关文章:

objective-c - 动画多个 Sprite Cocos2d

iphone - iphone 项目中的 Chartboost/iOS 中的 Chartboost 使用

android - Cocos2d Android 锁屏或退出游戏强制重启

cocoa-touch - 有没有办法在运行时获取Bundle ID?

ios - DefinesPresentationContext/UIModalPresentationCurrentContext 的问题 - 当前上下文 View Controller 丢失

iphone - Cocos2d。一个带有粒子的 CCLayer 实例适用于很多场景?

cocos2d-android - 如何在android cocos2d中存储游戏分数

android - 圆形图像切割形式所有侧面

ios - 我需要在 iOS 类中声明 UIApplication SharedApplication 多少次?

ios - 如何将 UIView 固定到屏幕底部?