★ TouchArcade needs your help. Click here to support us on Patreon.

A little coding help please?

01-22-2011, 02:03 PM
#1
Joined: Apr 2010
Posts: 57
A little coding help please?

Hey guys I'm embarrassed a little to ask but I'm really stuck and don't know what else to do. I've just created my first game using Cocos2D. It's completely done except I can't get the background to scroll giving the illusion of depth/movement. It's a very simple game so there isn't much code - it's my first after all.

I've spent a week solid every night after my kids/wife are in bed and I am frustrated beyond belief. I'm hoping there's some kind soul who I can share a stripped down version of my project with who might be able to look at it and let me know what I'm doing wrong.

Thanks in advance to anyone that might offer to help out.
01-25-2011, 10:13 PM
#2
Joined: Oct 2010
Location: Canada
Posts: 3
Hello there! First and foremost: kudos for having the drive to cram in development where ever possible. I hope that, barring this issue, your game does well in the marketplace. Or, it being your first game, that you learn a lot in the process. =)

As for your problem with background scrolling, would you be willing to post some of your code here? Perhaps just the code relevant to the background scrolling. It might also be helpful if you know of any other games (or YouTube videos) that have a scrolling effect that is similar to what you are trying to produce.

01-26-2011, 01:33 PM
#3
Joined: Apr 2010
Posts: 57
Hi Multihead, thanks for the reply.

Basically it's a static 320x480 image that I'm trying to slide down the screen and once it leaves the bottom of the screen to be redrawn and repeat the same movement again from above.

Something like this:
http://www.youtube.com/watch?v=o5tH1P6cv8Q

I initially wanted to add clouds too but I need to get over this hurdle first.

Here's the code I'm trying to get my background to move. A stripped down but completely runnable project is here if that helps more. I'm stuck and even thought about changing my game mechanics to not require any moving background.

Code:
//***********  This is code I'm trying to get the background to move
		

		
		// Background
		backgroundSprite = [CCSprite spriteWithFile:@"Background.png"
											   rect:CGRectMake(0.0f, 0.0f, 
															   320.0f, 480.0f)];
		ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
		[backgroundSprite.texture setTexParameters:&params];
		backgroundSprite.anchorPoint = CGPointZero;
		[self addChild:backgroundSprite z:-5];

		CGPoint newPosition = ccp(self.position.x, self.position.y - 0.0f);
		[self  runAction:[CCSequence actions:
						  [CCMoveTo actionWithDuration:2.0f position:newPosition],
						  nil]];

		
		CCLOG(@"Moved Layer to X: %f, Y: %f",
			  self.position.x, self.position.y);
01-26-2011, 03:24 PM
#4
Joined: Sep 2009
Posts: 2,240
The first thing I see is that when setting the y-coordinate to your newPosition, you are subtracting 0.0f. So you aren't actually changing the y-coordinate, which means there is no newPosition to move to.

In the game I'm working on now, we have three backgrounds at the moment, and I ended up using the CCParallaxNode object to handle the parallax for me. But that might be a bit overkill for you with only one background image.

You might also run into an issue with the texture rectangle you make. If you get the background to scroll, and it goes to black, then try multiplying the 480.0f y-coordinate of your rectangle by some multiplier.

I hope that helps!

Quote:
Originally Posted by Vidkid72 View Post
Hi Multihead, thanks for the reply.

Basically it's a static 320x480 image that I'm trying to slide down the screen and once it leaves the bottom of the screen to be redrawn and repeat the same movement again from above.

Something like this:
http://www.youtube.com/watch?v=o5tH1P6cv8Q

I initially wanted to add clouds too but I need to get over this hurdle first.

Here's the code I'm trying to get my background to move. A stripped down but completely runnable project is here if that helps more. I'm stuck and even thought about changing my game mechanics to not require any moving background.

Code:
//***********  This is code I'm trying to get the background to move
		

		
		// Background
		backgroundSprite = [CCSprite spriteWithFile:@"Background.png"
											   rect:CGRectMake(0.0f, 0.0f, 
															   320.0f, 480.0f)];
		ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
		[backgroundSprite.texture setTexParameters:&params];
		backgroundSprite.anchorPoint = CGPointZero;
		[self addChild:backgroundSprite z:-5];

		CGPoint newPosition = ccp(self.position.x, self.position.y - 0.0f);
		[self  runAction:[CCSequence actions:
						  [CCMoveTo actionWithDuration:2.0f position:newPosition],
						  nil]];

		
		CCLOG(@"Moved Layer to X: %f, Y: %f",
			  self.position.x, self.position.y);

Last edited by mikeg123; 01-26-2011 at 04:09 PM.
01-26-2011, 04:00 PM
#5
Joined: Jan 2010
Location: Vegas
Posts: 5
Send a message via Skype™ to JaredJudd
Another problem might be the texture you are applying. GLRepeat only works with images sized with Certain dimensions. (128x512, 512x1024, 16x32, etc). If you wanted you could set the content size to repeat upwards, and then need to 'move' it less frequently.
01-26-2011, 05:29 PM
#6
Joined: Apr 2010
Posts: 57
Thanks guys, I tried changing the -y and it moves down one whole screen but isn't actually on its own layer as it moves everything.

I did try changing my background image to a texture with a multiple of 2 but that didn't change anything.

Thanks for the advice so far guys, I'm close...
01-26-2011, 09:09 PM
#7
Joined: Jan 2010
Location: Vegas
Posts: 5
Send a message via Skype™ to JaredJudd
Change:

Code:
[self  runAction:[CCSequence actions: [CCMoveTo actionWithDuration:2.0f position:newPosition], nil]];
to reference the correct object:

Code:
[backgroundSprite  runAction:[CCSequence actions: [CCMoveTo actionWithDuration:2.0f position:newPosition], nil]];
01-26-2011, 09:52 PM
#8
Joined: Apr 2010
Posts: 57
Thanks Jared! I really appreciate the help. Just for kicks and to get this working, I changed my background sprite to 128x512 to try it to no avail. As for the code I have now changed it to this:

Code:
		// Background
		backgroundSprite = [CCSprite spriteWithFile:@"Background.png"
											   rect:CGRectMake(0.0f, 0.0f, 
															   320.0f, 480.0f)];
		ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
		[backgroundSprite.texture setTexParameters:&params];
		backgroundSprite.anchorPoint = CGPointZero;
		[self addChild:backgroundSprite z:-5];

		CGPoint newPosition = ccp(self.position.x, self.position.y - 480.0f);
		[backgroundSprite runAction:[CCSequence actions:
						  [CCMoveTo actionWithDuration:2.0f position:newPosition],
						  nil]];

		
		CCLOG(@"Moved Layer to X: %f, Y: %f",
		  self.position.x, self.position.y);
Yet when I reference the backgroundSprite instead of self then no movement happens. I know it's something stupid that I'm not seeing.