A little coding help please?

Discussion in 'Public Game Developers Forum' started by Vidkid72, Jan 22, 2011.

  1. Vidkid72

    Vidkid72 Well-Known Member

    Apr 11, 2010
    57
    0
    0
    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.
     
  2. Multihead

    Multihead New Member

    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.
     
  3. Vidkid72

    Vidkid72 Well-Known Member

    Apr 11, 2010
    57
    0
    0
    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);
     
  4. mikeg123

    mikeg123 Well-Known Member

    Sep 23, 2009
    2,242
    0
    36
    #4 mikeg123, Jan 26, 2011
    Last edited: Jan 26, 2011
    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! :)

     
  5. JaredJudd

    JaredJudd Member

    Jan 5, 2010
    5
    0
    0
    Lead Software Engineer
    Vegas
    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.
     
  6. Vidkid72

    Vidkid72 Well-Known Member

    Apr 11, 2010
    57
    0
    0
    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. :eek:

    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...
     
  7. JaredJudd

    JaredJudd Member

    Jan 5, 2010
    5
    0
    0
    Lead Software Engineer
    Vegas
    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]];
     
  8. Vidkid72

    Vidkid72 Well-Known Member

    Apr 11, 2010
    57
    0
    0
    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.
     

Share This Page