King Popsicle
King Popsicle

Reputation: 465

How to call function from one class in another class

I have 2 classes ThisWillWorkLayer and ThisWillWorkScene

ThisWillWorkLayer.m

#import "ThisWillWorkLayer.h"
#import "ThisWillWorkScene.h"


@implementation ThisWillWorkLayer


#define kSwipeScale 0.6

-(void) ccTouchMoved: (UITouch *)touch withEvent: (UIEvent *)event {


    CGPoint touchPoint = [touch locationInView:[touch view]];
    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];

    NSLog(@"Touch at x:%d y:%d",(int)touchPoint.x,(int)touchPoint.y);

    [ThisWillWorkScene rotate:touchPoint];
}


@end

The ccTouchMoved function is executed whenever a swipe is made. My error is on the last line of the function. [ThisWillWorkScene rotate:touchPoint]; it says the rotate function does not exsist. However if you look at the ThisWillWorkScene class it seems like it does

ThisWillWorkScene.h

#import "CC3Scene.h"

/** A sample application-specific CC3Scene subclass.*/
@interface ThisWillWorkScene : CC3Scene {
    CGPoint lastPoint;
    CC3Camera* cam;

}

-(void) rotate: (CGPoint) touchPoint;

@end

ThisWillWorkScene.m

@implementation ThisWillWorkScene


#define kSwipeScale 0.6

-(void) rotate: (CGPoint) touchPoint{

    //CC3Camera* cam = self.activeCamera;

    // Get the direction and length of the movement since the last touch move event, in
    // 2D screen coordinates. The 2D rotation axis is perpendicular to this movement.
    CGPoint swipe2d = ccpSub(touchPoint, lastPoint);
    CGPoint axis2d = ccpPerp(swipe2d);

    // Project the 2D axis into a 3D axis by mapping the 2D X & Y screen coords
    // to the camera's rightDirection and upDirection, respectively.
    CC3Vector axis = CC3VectorAdd(CC3VectorScaleUniform(cam.rightDirection, axis2d.x),
                                  CC3VectorScaleUniform(cam.upDirection, axis2d.y));
    GLfloat angle = ccpLength(swipe2d) * kSwipeScale;

    // Rotate the cube under direct finger control, by directly rotating by the angle
    // and axis determined by the swipe. If the die cube is just to be directly controlled
    // by finger movement, and is not to freewheel, this is all we have to do.
    CC3MeshNode* helloTxt = (CC3MeshNode*)[self getNodeNamed: @"Hello"];
    [helloTxt rotateByAngle: angle aroundAxis: axis];

    lastPoint = touchPoint;

}


@end

So my question is what is the proper way to call the rotate function from within ThisWillWorkLayer

Upvotes: 0

Views: 129

Answers (3)

GoZoner
GoZoner

Reputation: 70135

Your [ThisWillWorkScene rotate:touchPoint] is trying to invoke an instance method on the ThisWillWorkScene class. For this to work, your rotate: method would need to be defined with +, as a class method, as such:

+ (void) rotate ...

Based on the content of your rotate: method, it looks as if it could be a class method.

If your intention is to use an instance of the ThisWillWorkScene class, then it would look like:

ThisWillWorkScene *scene = [[ThisWillWorkScene] alloc] init];
[scene rotate:touchPoint];

Upvotes: 2

Mohannad A. Hassan
Mohannad A. Hassan

Reputation: 1648

The method is declared as an instance method. You need to call it on an instance of ThisWillWorkScene.

If you don't need to make it an instance method, then declare it as a class method.

//in .h
+(void) rotate: (CGPoint) touchPoint;// Notice the plus

//in .m
+(void) rotate: (CGPoint) touchPoint{
    //Code
}

Upvotes: 3

Undo
Undo

Reputation: 25687

Your rotate: method is an instance method, not a class method. To call an instance method, you need to have an instance of that object to call it on.

For example, this code would work with your current setup:

ThisWillWorkScene *scene = [[ThisWillWorkScene alloc] init]; //Get an instance, you might have to change this
[scene rotate: touchPoint];

If you wanted to be able to call a class method on the object (which I don't think is what you want to do), then you would change

-(void) rotate: (CGPoint) touchPoint;

to

+(void) rotate: (CGPoint) touchPoint;

Upvotes: 3

Related Questions