开发者

Loading and Unloading background images as game moves

开发者 https://www.devze.com 2023-02-11 13:32 出处:网络
Hey guys, I have a raidan style 开发者_运维技巧2d flying/fighter game that im working on. Upon moving it from the standard NSObject framework to Cocos2D, I have encountered an issue with my background

Hey guys, I have a raidan style 开发者_运维技巧2d flying/fighter game that im working on. Upon moving it from the standard NSObject framework to Cocos2D, I have encountered an issue with my background image/sprite.

Before cocos2d, my background was very large vertically. 320x9999 (or something). But it loaded and it created the illusion for a very large level. Cocos2d has limitations on its image sizes, so I was forced to slice up the image into 33 different background images each at 320x480.

Understanding that memory is a big deal on the iOS platform, I decided that I would create 2 background CCSprites: CCSprite *background1, *background2; Background2 ontop of background1.

Using these two backgrounds, as the Jet moves up the screen, each background will move inversely down the screen at an equal rate, As the first background1 moves completely off the screen vertically, it is released back into the memory and a new sprite is loaded into background1 and background1 is then positioned above background2 vertically.

As background2 moves off the screen, it is released and a new image is loaded into background2 and it is placed ontop of background1.

Creating the illusion of a constant and consistent background.

Q. Does this seem like a valid method? Q. How could I go about achieving something like this programatically? I have the physics of movement and everything ready to go. Im just a little unsure how to do this with Cocos.

Once again, thanks in advance!


A1) yes it's a valid way to do so, and it's almost very easy thing to do ht ecode goes somthing like this (i may have some programatical errors ,consider this almost as a psudo code!):

if(background[1].position.y < 0)
{
    [layer removeChild:background[1] cleanup:YES];
    background[1] = background [2];
    background[2] = [CCSprite SpriteWithFile:"file id"]
    background[2].position = ccp(0,480);
    [background[2] runAction:[CCMoveBy actionWithDuration:1 position:ccp(0,1)]]
}

A2) if you only needed some guide to do so i gave you an implementation for what you need and if you mean you don't know how to implement plysics stuff using cocos2d-iphone try these manuals :

http://www.raywenderlich.com/457/intro-to-box2d-with-cocos2d-tutorial-bouncing-balls

http://www.raywenderlich.com/475/how-to-create-a-simple-breakout-game-with-box2d-and-cocos2d-tutorial-part-12

http://www.raywenderlich.com/505/how-to-create-a-simple-breakout-game-with-box2d-and-cocos2d-tutorial-part-22


I'm not absolutely sure about what you say, I think you are talking about Swapping Backgrounds, one background becomes active the other takes the position of next part. This is not an uncommon technique, ideas similar to this are used in many ways. Also search for clip maps, mega textures, atlas texture.

0

精彩评论

暂无评论...
验证码 换一张
取 消