2013-06-07 2 views
0

J'ai 2 classes ThisWillWorkLayer et ThisWillWorkSceneComment appeler la fonction d'une classe dans une autre classe

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 

La fonction ccTouchMoved est exécutée chaque fois un simple glissement est faite. Mon erreur est sur la dernière ligne de la fonction. [ThisWillWorkScene rotate:touchPoint]; il dit que la fonction de rotation n'exsiste pas. Toutefois, si vous regardez la classe ThisWillWorkScene il semble que cela ne

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 

Alors ma question est quelle est la bonne façon d'appeler la rotate fonctionner à partir de ThisWillWorkLayer

Répondre

3

Votre méthode rotate: est une instance remplie hod, pas une méthode classe. Pour appeler une méthode d'instance, vous devez avoir une instance de cet objet pour l'appeler.

Par exemple, ce code fonctionne avec votre configuration actuelle:

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

Si vous voulez être en mesure d'appeler une méthode de classe sur l'objet (que je ne pense pas est ce que vous voulez à faire), alors vous changeriez

-(void) rotate: (CGPoint) touchPoint; 

à

+(void) rotate: (CGPoint) touchPoint; 
3

La méthode est déclarée en tant que méthode d'instance. Vous devez l'appeler sur une instance de ThisWillWorkScene.

Si vous n'avez pas besoin d'en faire une méthode d'instance, déclarez-la en tant que méthode de classe.

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

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

Votre [ThisWillWorkScene rotate:touchPoint] tente d'invoquer une méthode d'instance de la classe ThisWillWorkScene. Pour que cela fonctionne, votre méthode rotate: devrait être défini avec +, en tant que méthode de classe, en tant que tel:

+ (void) rotate ... 

Basé sur le contenu de votre méthode rotate:, il semble que cela pourrait être une méthode de classe .

Si votre intention est d'utiliser une instance de la classe il, alors ThisWillWorkScene ressembler à:

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