xcode Cocos2D 垂直滚动背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5474300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cocos2D vertically scrolling background
提问by John
I have three images (320x480) that I'm trying to scroll vertically in my Cocos2D application.
我有三个图像 (320x480),我试图在我的 Cocos2D 应用程序中垂直滚动它们。
In my initialization method, I have the following:
在我的初始化方法中,我有以下内容:
//adding background sprites
background = [CCSprite spriteWithFile:@"BG1.png"];
background2 = [CCSprite spriteWithFile:@"BG2.png"];
//position background sprites
background.position = ccp(size.width, size.height/2);
background2.position = ccp(size.width, size.height*2);
//schedule to move background sprites
[self schedule:@selector(scroll:)];
//adding them to the main layer
[self addChild:background z:0];
[self addChild:background2 z:0];
And here's my scroll method:
这是我的滚动方法:
-(void) scroll:(ccTime)dt
{
//move 30*dt px vertically
background.position = ccp(background.position.x, background.position.y - 30*dt);
background2.position = ccp(background2.position.x, background.position.y - 30*dt);
//reset offscreen position
if (background.position.y < 290)
{
background.position = ccp(480/2, 480);
}else if (background2.position.y < 290)
{
background2.position = ccp(480/2,480);
}
}
Currently what's happening is my first background image is offset by about a quarter of the screen (horizontally), and it starts a quarter of the way up from the bottom of the screen, but it scrolls down. My second background image doesn't actually spawn, the first image just loops over and over while being offset. Is there any way to make the two images smoothly loop continuously in the background, and how would I incorporate a third image?
目前发生的事情是我的第一个背景图像偏移了大约四分之一屏幕(水平),它从屏幕底部向上四分之一处开始,但它向下滚动。我的第二个背景图像实际上并没有产生,第一个图像只是在偏移时一遍又一遍地循环。有什么方法可以让两张图片在背景中连续平滑循环,我将如何合并第三张图片?
Also, just a quick side question, is it bad to name objects (I think they're objects) with numbers in their name (ie background2/background3)?
另外,只是一个简单的附带问题,用名称中的数字命名对象(我认为它们是对象)是否不好(即背景2/背景3)?
回答by Jeremie D
Tested for horizontal scrolling in landscape mode (all you have to do is change the scrolling from horizontal to vertical, you should be able to figure this out) dont forget that ccposition is from the middle of the sprite, not from 0,0 perspective...:
在横向模式下测试水平滚动(您所要做的就是将滚动从水平更改为垂直,您应该能够弄清楚这一点)不要忘记 ccposition 来自精灵的中间,而不是从 0,0 的角度。 ..:
CGSize size = [CCDirector sharedDirector].winSize;
//adding background sprites
background = [CCSprite spriteWithFile:@"tracktest.png"];
background2 = [CCSprite spriteWithFile:@"tracktest.png"];
[background.texture setAliasTexParameters];
[background2.texture setAliasTexParameters];
//position background sprites
background.position = ccp(background.contentSize.height/2,background.contentSize.width/2);
background2.position = ccp(size.width,0);
//schedule to move background sprites
[self schedule:@selector(scroll:)];
//adding them to the main layer
[self addChild:background z:0];
[self addChild:background2 z:0];
-scroll method:
-滚动方法:
-(void) scroll:(ccTime)dt
{
//move 30*dt px vertically
if (background.position.x<background2.position.x){
background.position = ccp(background.position.x - 30*dt,background.contentSize.height/2);
background2.position = ccp(background.position.x+background.contentSize.width,background2.contentSize.height/2);
}else{
background2.position = ccp(background2.position.x- 30*dt,background2.contentSize.height/2);
background.position = ccp(background2.position.x+background2.contentSize.width ,background.contentSize.height/2);
}
//reset offscreen position
if (background.position.x <-background.contentSize.width/2)
{
background.position = ccp(background2.position.x+background2.contentSize.width,background.contentSize.width/2);
}else if (background2.position.x < -background2.contentSize.width/2)
{
background2.position = ccp(background.position.x+background.contentSize.width, background2.contentSize.width/2);
}
}