How to add animated background - cocos2d-iphone

i have a gif image and i want to put it in my iphone game background, the image is a moving scenery but when i put in my background its not moving ..How can i keep my background moving like the gif image?

FOr that you have to use the imageview and set its frame as background view.
Insert all your images used for creating GIF in Resource folder
Then use the following code
UIImageView *imgBackView=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,320,480)];
NSArray *myImages = [NSArray arrayWithObjects:#"1.png",#"2.png",#3.png"#4.png", nil];
imgBackView.animationImages = myImages;
imgBackView.animationDuration = 0.5;// OR WHATEVER TIME YOU WANT
imgBackView.animationRepeatCount = 0; // 0 = loops forever
[imgBackView startAnimating];
[self.view addSubview:imgBackView];
[imgBackView release];
hAPPY iCODING...

As far as I know, animated gifs does not work in iPhone. However it is possible to have the same effect using animationImages property of UIImageView. Instead of setting single image, you can set array of UIImages as animationImages of the view. Then you can start the animation any time by calling startAnimating method.
Of course to have an array of images, you will have to extract the frames of the animated gif as different images and add them to your resources.

Related

How to do overlapping custom items in qt?

Like in the title. I have few objects, and i wanted to set graphics to them and overlap each other. I'm setting 18 squares (terrains). I'm adding to this terrains graphics:
setPixmap(QPixmap(":Graphics/Terrain")); like so.
Then i want to add frame to it. I have 4 files of format .png, every one is 200x200px. First one is terrain.png - it's a green square, second and third are frames, about 20px wide with a transparent center (with alpha channel) and the last .png file is City.png, in left corner of City.png, there is a small graphics, rest is transparent (alpha channel). When i'm trying to set the frame by the same way as terrain.png:
setPixmap(QPixmap(":Graphics/Frame"));
it covers all of the terrain.png graphics, despite the fact that the center of frame1.png is transparent.
Furthermore i want to add some others object like for example City.png. How to do it? Mayby exists some diffrent way except using this setPixmap function ?
The setPixmap approach seems to work quite nicely, when used like this:
QGraphicsScene* scene = new QGraphicsScene();
QGraphicsPixmapItem* itemA = new QGraphicsPixmapItem();
itemA->setPixmap(QPixmap(":/NonTransparentImage.png"));
scene->addItem(itemA);
QGraphicsPixmapItem* itemB = new QGraphicsPixmapItem();
itemB->setPixmap(QPixmap(":/TransparentImage.png"));
scene->addItem(itemB);
QGraphicsView* view = new QGraphicsView(scene);
view->show();
As you expect, the transparent itemB overlaps itemA.

Stars for scrolling shooter with CCParticleSystemQuad?

My code for init function:
NSArray *starsArray = [NSArray arrayWithObjects:#"Stars1.plist", #"Stars2.plist", #"Stars3.plist", nil];
for(NSString *stars in starsArray) {
CCParticleSystemQuad *starsEffect = [CCParticleSystemQuad particleWithFile:stars];
[self addChild:starsEffect z:-1];
}
The problem is that these particles appear and fully fill the screen rectangle during few seconds. But I need the sky full of stars from the beginning.
According to the answer at cocos2d starting particles from a specific time in the future , you can manually update the particle system. Example in cocos2d-x:
CCParticleSystemQuad *particle = CCParticleSystemQuad::create("res/Particles/Stars1.plist");
for (int i = 0; i < 10; ++i) {
particle->update(.1);
}
You may need to change the interval to suit the particles.
Add them to a layer, hide the layer, then unhide the layer after everything is done loading. That way you can set stuff up and not have it display right away.
That's just one approach. Another idea is to load all your images into Cocos before game play and game logic processes begin. That way there is no pause and delay while images are loading.

How to load sprites in cocos2d correctly?

my problem is:
I'm making a game for iOS using cocos2d and this game has lots of levels, so I'll have to create a loading scene to load my sprites for each level. ( like new backgrounds, monsters and other stuff )
But I have no idea about this, I'm adding all the Texture Packer Files (.plist and .pvr.ccz)
on the sharedSpriteFrameCache in the GameData.m.
Does anyone knows a good tutorial for this or can help me solve this?
Thanks!
So basically you want to know how to load and unload images as you see fit. How about
#implementation Level1
- (void) loadLevel
{
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCTextureCache* textureCache = [CCTextureCache sharedTextureCache];
// Add the sprite frames. This will load the texture as well
[frameCache addSpriteFramesWithFile:#"monkey.plist"];
[frameCache addSpriteFramesWithFile:#"player.plist"];
[frameCache addSpriteFramesWithFile:#"enemy.plist"];
// Load other textures that are going to be used
_myBackgroundTexture = [textureCache addImage:#"background.png"];
}
- (void) unloadLevel
{
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCTextureCache* textureCache = [CCTextureCache sharedTextureCache];
// Remove textures
[textureCache removeTexture:_myBackgroundTexture];
// Remove sprite frames. This will load the texture as well
[frameCache removeSpriteFramesFromFile:#"monkey.plist"];
[frameCache removeSpriteFramesFromFile:#"player.plist"];
[frameCache removeSpriteFramesFromFile:#"enemy.plist"];
// Though normally, id use frameCache removeUnusedSpriteFrames and
// textureCache removeUnusedTextures
}
...
#end
You can use the sprite by CCMenuItem and also by the Menu as you would require to click and move to that particular level.. The below is the code for adding the menu item image
CCMenuItem *m4 =[CCMenuItemImage itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
selectedSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
disabledSprite:[CCSprite spriteWithSpriteFrameName:<#(NSString *)spriteFrameName#>]
target:self selector:#selector(MoveLeft)];
The above code gives the information you can use to display the sprite for the particular state of the menu. And the "MoveLeft" is the method selector which I have used to call the particular method.
At last you can add the m4 object to the CCMenu and get the desired output...
Hope it works for you.

How to show part of an image using QT?

So, this is my problem: I have this very big image, and I want to show only a specific part of it. After the user pressing a specific key I want the image to move, showing another part of it. The transition from one part of the image to another have to be smooth, animated.
I tried using a QLabel to show the image but it always shows the center of the image, and I do not really know how to make the animation. What would you guys suggest?
Interesting question. Here is something I just tested and seems to work.
Add a QGraphicsView with dimensions the dimensions of the part of the image you want to display, eg 100x100. Create a QGraphicsScene and add it to the view:
QGraphicsScene* pScene = new QGraphicsScene(this);
ui->graphicsView->setScene(pScene);
Now add your image into the scene. In my case I has an image in my resource file. The trick is to set the sceneRect to the position you want to display. I wanted to display an 100x100 part of the image starting from 0,300 :
pItem = pScene->addPixmap(QPixmap::fromImage(QImage(":/photos/image")));
pScene->setSceneRect(0,300,100,100);
In order to test the smooth moving I added a button which when clicked is triggering a slot called move. This slot simply updates the sceneRect. In my simple example I just move the image 100 pixels right. In a real world scenario you could also move it diagonally or vertically and check the image limits.
void move()
{
for (unsigned i=currentX; i<currentX + 100; i++)
{
ui->graphicsView->scene()->setSceneRect(i,300,100,100);
qApp->processEvents();
}
currentX += 100;
}
Notice the currentX variable. It is nothing more than the last image position. Also we must call the processEvents in order to "see" the image moving smoothly.
You could use QPixmap::copy( int x, int y, int width, int height ) to copy a region of the image and display that.
Few options:
Try using a Q3CanvasSprite (within in a Q3Canvas). It is designed more for splitting one image into multiple ones but not for animating between them. You could try abusing it and declaring (say) 100 frames (10 per digit, which would be used as animation steps) or just use the move() method.
Try QGraphicsPixmapItem::setOffset() (within a QGraphicsScene). This might be overkill as QGraphicsScene is made for large number of images).
I'm not sure, but maybe this can be done with QStateMachine and QAbstractAnimation.

SDL delete an image and replace it with the new one

I've started SDL just a few days ago and I'm having a problem
I tried to erase an image from the screen and replace it with the new one
here is my logic :
load image
apply surface, then delay for 1s
free old image surface (SDL_FreeSurface())
load new image
apply surface
The problem is the image is still there. (it didn't get erased, just stacked with the new image)
The screen doesn't work like you think it does. You cannot "delete" something from a screen buffer, you can only write new things to the screen buffer. To 'erase' something you need to write the "background" over it.
Some game loops just re-write the entire screen with the background every frame.
This probably belongs over at gamedev.stackexchange.com