【问题标题】:Getting location of sprite within array cocos2d`获取数组 cocos2d 中精灵的位置`
【发布时间】:2012-07-02 00:38:16
【问题描述】:

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

数组:

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 的位置实际上并没有更新或移动。耶!只有在精灵的原始位置生成。

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    CCSprite *temp = [CCSprite spriteWithFile:@"taco.png"];
    
    temp = [birdLayer getChildByTag:100];
    
    if (temp.position.x == location.x) {
    
        // do stuff...
    }
    

    【讨论】:

    • 问题是鹦鹉精灵的位置没有更新。所以 parrot.position.x 保持在初始位置。
    • 试试这个 TouchableSprites 类。使用 SetCanTrack:YES 对我来说效果很好。 cocos2d-iphone.org/forum/topic/5971
    • 实际上精灵并没有移动,因为在迭代中,无论标签如何,最后一个精灵总是被调用的。
    • 精灵移动,就像 .png 在指定图层上移动一样。什么意思?
    • 我在迭代中体验过,redBirds sprite 编号 7 将是通过触摸检测到的,并接收 [self movement] 调用(因为它是 CCArray 中的最后一个)而不是数字 4,如您所愿。这就是 TouchableSprites 是处理触摸的好方法的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多