문제

i try to return a CGPoint but i do something wrong:

Here is my method:

- (CGPoint)calculatePointOnCircleFrom:(CGPoint)pointA PointB:(CGPoint)pointB radius:(float)rd {
    float sryy = pointA.y - pointB.y;
    float srxx = pointA.x - pointB.x;
    float sry = pointA.y + sryy;
    float srx = pointA.x + srxx;
    float kpx = pointA.x + cos(atan2(pointA.y - sry, pointA.x - srx)) * rd;
    float kpy = pointA.y + sin(atan2(pointA.y - sry, pointA.x - srx)) * rd;

    return CGPointMake(kpx, kpy);
}

The code in the method works fine but i do something wrong with the initialization.

Here i call the method:

    point1.position = [self calculatePointOnCircleFrom:Player.position PointB:touchPos radius:64]; 

and get fooling error: "incompatible type for argument 1 of 'setPosition:'"

도움이 되었습니까?

해결책

There's probably also a warning that it can't find the method declaration. It sounds like the compiler doesn't know what type the method returns and is defaulting to id.

다른 팁

If you look closely you'll see that the message is about an incompatible type for argument 1 of a method called setPosition, not the calculatePointOnCircleFrom method. The setPosition is the setter for the position property.

So the problem is with the point1.position = part of the line, not the call to the calculatePointOnCircleFrom method. I suspect the position property of the point1 variable is not of type CGPoint since that's what the calculatePointOnCircleFrom method is returning.

Alternatively, you may not be calling the method you think you're calling.

That error message is telling you that the type returned by [self calculatePointOnCircleFrom:Player.position PointB:touchPos radius:64] does not match the type of point1.position. Is position a CGPoint?

I solved the problem on a not perfect way but it works.

I return know a NSValue:

- (NSValue *)calculatePointOnCircleFrom:(CGPoint)pointA PointB:(CGPoint)pointB radius:(float)rd {
   float sryy = pointA.y - pointB.y;
   float srxx = pointA.x - pointB.x;
   float sry = pointA.y + sryy;
   float srx = pointA.x + srxx;
   float kpx = pointA.x + cos(atan2(pointA.y - sry, pointA.x - srx)) * rd;
   float kpy = pointA.y + sin(atan2(pointA.y - sry, pointA.x - srx)) * rd;

   return [NSValue valueWithCGPoint:CGPointMake(kpx, kpy)];
}

and call method like so:

NSValue *pos = [self calculatePointOnCircleFrom:Player.position PointB:touchPos radius:64];
CGPoint cgpos = [pos CGPointValue];
point1.position = cgpos;

Thanks for every ones help :D

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top