【发布时间】:2013-09-08 16:36:48
【问题描述】:
我来这里是想问一个菜鸟问题,我正在学习用 cocos2d 滚动。
我的问题是,用场景在图层之间进行通信的最佳方式(以及如何)是什么?
例如。
我有一个图层,其中有一个带有按钮的精灵,还有一个带有字符串的图层。
每次我单击按钮时,字符串都应该为 +1。 (所以如果你点击 3 次,字符串将等于 3。)
我有这样的:
场景.m
-(id)init {
self = [super init];
if(self != nil){
//button Layer
buttonLayer *buttonLayer = [buttonLayer node];
[self addChild:buttonLayer z:0];
//Gameplay Layer :D
stringLayer *numberStringLayer = [stringLayer node];
[self addChild:numberStringLayer z:2];
}
}
buttonLayer.m
-(id)init {
int xPosition = 385;
int yPosition = 75;
_button = [CCMenuItemImage itemWithNormalImage:@"button.png"
selectedImage:@"button.png"
target:self selector:@selector(checkButton:)];
_button.tag =0;
_button.position = ccp(xPosition,yPosition);
_buttonMenu = [CCMenu menuWithItems:_button, nil];
_buttonMenu.position = CGPointZero;
[self addChild:_buttonMenu];
}
-(void)checkButton:(id)sender {
NSLog(@"Button Pressed");
buttonPressedCount =+;
//Here goes algorithm that interacts with scene/layer
}
stringLayer.m
-(id)init {
self = [super init];
if (self != nil) {
_numberString = [CCLabelTTF labelWithString:@"0" fontName:@"Marker Felt" fontSize:18.0];
_numberString.color = ccc3(0,0,0);
_numberString.position = ccp(125,300);
[self addChild:_numberString];
}
return self;
}
-(void)updateStringWithNumber:(int)tempNumb {
_numberString.string = tempNumb; //or something like that....
}
那么...我在哪里/如何转换变量以及如何/在哪里访问/调用它们?
感谢您的宝贵时间! :D 有一个美好的一天!
【问题讨论】:
-
您会选择哪些选项? UILayer *tempLayer = [场景 sharedGameScene].stringLayer; [临时图层更新字符串]; ???如果是这样,我在哪里/如何投射它?
-
@LearnCocos2D 我尝试获取 ChildBy Tag,但我根本不知道该怎么做。在scene.m上试过这个“stringLayer *numberStringLayer = [stringLayer node]; [self addChild:numberStringLayer z:2 tag:113];”当用户按下按钮时:“ CCNode *otherNode = [stringLayer.parent_ getChildByTag:113]; [otherNode updateLabel];”
标签: cocos2d-iphone layer scene