我的GameController.m调用CCNode类的方法。它似乎称之为我可以看到NSLog,但该方法既不操作也不导致视图。换句话说,从GameController调用方法时屏幕不会改变。 (方法在自己的类中运行)
我应该如何更改代码来修复它?
要点: GameController - >电话 - > GoalPost.GoalKeeper.method。
代码: GameController.m,GoalPost.m和GoalKeeper.m
GameController.m
#import "GameController.h"
#import "cocos2d-ui.h"
#import "GoalPost.h"
#import "Ball.h"
#import "Field.h"
@interface GameController()
@property Field* field;
//@property Ball* ball;
@property GoalPost* post;
@end
@implementation GameController
- (instancetype)init
{
self = [super init];
if (self) {
self.field = [Field node];
// self.ball = [Ball node];
self.post = [GoalPost node];
}
return self;
}
//control computer.
-(void)kickerTurn{
NSLog(@"Player kick");
//random location generation
//call keeper.
int randomKeeperX = arc4random() % (int)self.post.contentSize.width;
int randomKeeperY = arc4random() % (int)self.post.contentSize.height;
CGPoint keeperLoc = ccp(randomKeeperX, randomKeeperY);
[self.post.keeper keeperLocation:keeperLoc];
}
@end
GoalPost.h
#import "GoalKeeper.h"
@interface GoalPost : CCNodeColor { }
@property GoalKeeper* keeper;
@end
GoalPost.m
#import "GoalPost.h"
#import "GoalKeeper.h"
@interface GoalPost ()
@property NSArray* post;
@end
@implementation GoalPost
- (instancetype)init
{
self = [super initWithColor:[CCColor lightGrayColor]];
if (self) {
....
[self addChild:aPost];
}
//Call goal keeper and add.
//for touch to run goal keeper.
[self setUserInteractionEnabled:true];
self.keeper = [GoalKeeper node];
//Put it in front of goal post.
self.keeper.position = ccp(self.contentSize.width/2, 0);
[self addChild:self.keeper];
}
return self;
}
-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchLoc = [touch locationInNode:self];
NSLog(@"Goal touched: %f %f", touchLoc.x, touchLoc.y);
//Goal keeper call
[self.keeper keeperLocation:touchLoc];
}
@end
最后,GoalKeeper.m
.H #import“cocos2d.h”
@interface GoalKeeper : CCNodeColor { }
-(void)keeperLocation:(CGPoint)location;
@end
的.m
#import "GoalKeeper.h"
@interface GoalKeeper()
@property CGPoint touchLoc;
@end
@implementation GoalKeeper
- (instancetype)init
{
self = [super initWithColor:[CCColor blueColor]];
if (self) {
[self setUserInteractionEnabled:TRUE];
[self setContentSize:CGSizeMake(30, 70)];
}
return self;
}
-(void)keeperLocation:(CGPoint)location {
CGPoint offset = ccpSub(location, self.position);
int targetX = self.position.x + offset.x;
int targetY = self.position.y + offset.y;
CGPoint targetPosition = ccp(targetX, targetY);
CCActionMoveBy *actionMoveBy = [CCActionMoveBy actionWithDuration:0.5f position:offset];
[self runAction:actionMoveBy];
}
@end