在Objective-C iPhone游戏中一个接一个地放置两个背景? [英] Putting two backgrounds one after the other in objective-c iphone game?
问题描述
我正在制作一个iphone游戏
Hi I am creating an iphone game
在我的游戏层中,我有两个相同的背景图像,但我希望它们一个接一个地移动.
in my game layer, I have two identical background images but I want them to go one after the other.
一旦游戏动物(例如企鹅)越过第一个背景,我希望第一个背景走在第二个背景之后-成为连续的背景,直到游戏结束.
Once the game animal (eg a penguin) crosses the first background, I want the first background to go after the second one-- becoming a continuous background until the game is over.
我已经尝试了所有方法-用于循环,while循环等,但似乎无济于事
I have tried everything--- for loops, while loops and such but nothing seems to work
有人知道我该怎么做吗?
Does anyone have an idea for how I could go about doing this?
感谢您能为我提供的任何帮助.
Thank you for any help you can provide me.
这是我经过多次尝试后到目前为止所拥有的全部内容
this is all I have so far after many different tries
- (id) init
{
if((self = [super init]))
{
for ( int x = 0; x < 10000000; ++x)
{
CCSprite *bg = [CCSprite spriteWithFile:@"level1.png"];
[bg setPosition:ccp(160,240)];
ccTexParams params = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
[bg.texture setTexParameters:¶ms];
[self addChild:bg z:0];
CCSprite *bg2 = [CCSprite spriteWithFile:@"level1.png"];
[bg2 setPosition:ccp(320,480)];
ccTexParams param = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
[bg2.texture setTexParameters:¶m];
[self addChild:bg z:0];
}
推荐答案
如果背景完全相同,则您实际上不需要2张图像,您只需设置其中一张的纹理矩形位置,它就会连续移动.如果您在计时器上或在update方法中调用moveBackground,它将不断滚动.
If the backgrounds are the identical you don't actually need 2 images, you can just set the texture rect position of the one and it will continuously move. If you call moveBackground on a timer or in the update method it will continually scroll.
-(void)init
{
if((self=[super init]))
{
background = [CCSprite spriteWithFile:@"background.png"];
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
[background.texture setTexParameters:¶ms];
[self addChild:background z:0];
}
}
-(void)moveBackground
{
// Scroll background 5 pixels to the left
int speed = 5;
background.textureRect = CGRectMake(background.textureRect.origin.x-speed,
background.textureRect.origin.y,
background.textureRect.size.width,
background.textureRect.size.height);
}
这篇关于在Objective-C iPhone游戏中一个接一个地放置两个背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!