[英]how to parse int in a for loop to a method in xcode?
嗨,大家好,我要创建6个精灵,并将它们均匀地隔开,我编辑了从书中得到的一些代码,但目前停留在需要将精灵均匀地隔开的部分
[groundNode setPosition:CGPointMake(45, 20)];
这会将所有6个Sprite彼此堆叠在一起? 我怎样才能使它变成类似
[groundNode setPosition:CGPointMake(45*x, 20)];
其中x是从for循环中获取的int。 我的代码在底部列出。 非常感谢!!
-(id)init{
self = [super init];
if(self !=nil){
for(int x=0;x<6;x++){
[self createGround];
}
}
return self;
}
-(void) createGround{
int randomGround = arc4random()%3+1;
NSString *groundName = [NSString stringWithFormat:@"ground%d.png", randomGround];
CCSprite *groundSprite = [CCSprite spriteWithFile:groundName];
[self addChild:groundSprite];
[self resetGround:groundSprite];
}
-(void) resetGround:(id)node{
CGSize screenSize =[CCDirector sharedDirector].winSize;
CCNode *groundNode = (CCNode*)node;
[groundNode setPosition:CGPointMake(45, 20)];
}
第一步是构建这些方法以采用offsetIndex参数:
-(void) createGroundWithOffsetIndex: (int) offsetIndex {
-(void) resetGround: (CCNode *) node withOffsetIndex: (int) offsetIndex {
然后,在createGround中,将其传递给:
[self resetGround:groundSprite withOffsetIndex: offsetIndex];
并从循环中传递它:
for(int x=0;x<6;x++){
[self createGroundWithOffsetIndex:x];
}
最后,您知道将要使用的部分代码(在resetGround:withOffsetIndex:内部):(请注意+1,因为偏移量(按语义)从零开始)
[groundNode setPosition:CGPointMake(45 * offsetIndex+1, 20)];
一些注意事项:
仔细考虑这里需要进行多少次传递,然后尝试考虑一种改进的体系结构:如果要平铺相同的图像,也许createGround应该采用CGRect并负责填充那么多区域?
这只是水平的; 我将其作为传递CGPoints(或类似的{x,y}结构)作为offsetIndex的练习。
您的投放模式是边界线。 为什么将其作为id传递,然后将其转换为另一个本地var,而将其作为另一种类型呢? 我想那...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.