问题描述:

The error in Xcode reads: sending CGPoint (aka 'struct CGPoint') to parameter of incompatible type 'void *'

What I'm doing is:

CCSprite* coordinate= path[i];

CGPoint posSending = coordinate.position;

id handleTail = [CCCallFuncND actionWithTarget:self selector:@selector(handleTail:)data:posSending];

//handleTail

-(void)handleTail:(CGPoint)pos{

}

What's causing this error?

网友答案:

CCCallFuncND doesn't work with struct types (nor does it work well with ARC).

Use the block version instead, much easier, a lot more powerful:

CGPoint posSending = coordinate.position;
id handleTail = [CCCallBlock actionWithTarget:self block:^{
    // do something with posSending:
    someNode.position = posSending;
}];

FYI the error is because you try to assign a CGPoint type to a void* type. Even if you did use the address of the CGPoint by passing it with the address operator (&posSending) you would receive garbage in your handleTail method. By the time that method is called, the pointer to posSending is no longer valid since posSending was created on the stack.

One might think that malloc'ing the CGPoint would work, and it does - however it's not safe to use because there is no guarantee that the handleTail method is ever going to be called (due to scene changes, or stopAction calls). Therefore it's not guaranteed that the receiving method can actually free that malloc'ed pointer.

In addition, your handleTail method is declared incorrectly, it receives a void* parameter:

-(void)handleTail:(void*)data
{
}

All of this, one takeway: use blocks whenever and whereever you can.

相关阅读:
Top