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

查看:217
本文介绍了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.

我试过的是这样调用它:

What I have tried is this for calling it:

    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 structs)到一个方法,以一个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对象中检索点到NSValue *并调用getValue:

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天全站免登陆