Cocos2D:通过 CCCallFuncND 传递一个 CGPoint [英] Cocos2D: Passing a CGPoint through CCCallFuncND

查看:22
本文介绍了Cocos2D:通过 CCCallFuncND 传递一个 CGPoint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用动作调用方法的适当方式是什么,传递 CGPoint 参数的方法本身应该是什么样的?我尝试在网上查找示例,但运气不佳,所以我一直在猜测.

What is the appropriate way to call a method with an action, and what should the method itself look like for passing a CGPoint parameter? I've tried to look up examples online without much luck, so I've been pretty much guessing.

我尝试过这样称呼它:

    CGPoint spriteCoord = saveStation.sprite.position;

    id a1=[CCMoveTo actionWithDuration:.4 position:ccp(saveStation.sprite.position.x,saveStation.sprite.position.y)];
    id actionSaveStationReaction = [CCCallFuncND actionWithTarget:self selector:@selector(saveStationReaction : data:) data:&spriteCoord];

    [hero.heroSprite runAction:[CCSequence actions:a1, actionSaveStationReaction, nil]];

还有方法本身:

-(void) saveStationReaction:(id)sender data:(void *)data {

    CGPoint spriteCoord = (void *)data; //error: Invalid initializer 

    NSLog(@"spriteCoord x = %f", spriteCoord.x);
    NSLog(@"spriteCoord y = %f", spriteCoord.y);

}

推荐答案

将 CGPoint(或任何非 id 类型,如 C 结构)发送到以 id 作为参数的方法(任何使用 performSelector 的方法)的正确方法) 是将其包装在一个 NSValue 对象中:

The proper way to send a CGPoint (or any non-id type like C structs) to a method that takes an id as parameter (any method that uses performSelector) is by wrapping it in an NSValue object:

NSValue* value = [NSValue valueWithBytes:&spriteCoord objCType:@encode(CGPoint)];

在被调用的方法中,您可以通过将数据指针转换为 NSValue* 并调用 getValue 从 NSValue 对象中检索点:

In the method that is being called you can retrieve the point from the NSValue object by casting the data pointer to NSValue* and calling getValue:

-(void) saveStationReaction:(id)sender data:(void *)data {

    CGPoint spriteCoord;
    [((NSValue*)data) getValue:&spriteCoord];

    NSLog(@"spriteCoord x = %f", spriteCoord.x);
    NSLog(@"spriteCoord y = %f", spriteCoord.y);

}

这篇关于Cocos2D:通过 CCCallFuncND 传递一个 CGPoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆