Repeating Sprite Cocos2d 3.0 - cocos2d-iphone

Im trying to make a scrolling affect in the background of my app
The sprite moves from north to south
This is successful but how can I repeat the sprite sprite to continuously do this? The sprite moves south and the end of the png file is seen, how could it be made that it repeats at this point so the sprite never ends? Thank you!
- (void) Buildings
{
rightBuilding = [CCSprite spriteWithImageNamed:#"rightBuilding.png"];
rightBuilding.positionType = CCPositionTypeNormalized;
rightBuilding.position = ccp(0.9f, 0.5f);
[self addChild:rightBuilding];
}
- (void) scrollBuildings:(CCTime)dt
{
rightBuilding.position = ccp(rightBuilding.position.x, rightBuilding.position.y - .5);
}

Related

Cocos2D scale animation

I'm trying to do something pretty simple in Cocos2D and running into trouble. I would like to scale a CCNode with animation.
In the code below you will see what is happening
// load object of CCNode subclass GameOver
_gameOver = (GameOver*)[CCBReader load:#"gamePlayItems/GameOver"];
[self addChild:_gameOver];
_gameOver.positionType = CCPositionTypeNormalized;
_gameOver.position = ccp(.5, .75);
At this point the CCNode is on the screen at the proper position
//scale it up
_gameOver.scale = .2;
id scale = [CCActionScaleTo actionWithDuration:.8 scale:1];
CCActionEaseBackOut* bounce = [CCActionEaseBackOut actionWithAction:scale];
[_gameOver runAction:bounce];
The animation is never fired. The GameOver object is on the stage scaled to .2
Any suggestions would be greatly appreciated.
Thanks!

How to set the position of a sprite to the center of the screen

I have some code on a pong game i've been working on:
The code sample shows that when the ball hits the bottom of the window, the ball resets to the middle of the window. What I'm trying to achieve is that I'd like to be able to add some code so that if the ball hits the top of the window, the ball will reset back to the middle.
Can anyone help me with this?
//...
// IF BALL GETS PAST PLAYER PADDLE AND TOUCHES BOTTOM OF SCREEN
if (GetPosition().y - GetHeight()/2 <= 0)
{
_angle = 180 - _angle;
moveByY = -moveByY;
}
if (GetPosition().y + GetHeight()/2 + moveByY >= Game::SCREEN_HEIGHT)
{
// RESET BALL TO MIDDLE OF SCREEN AND RESET TIMER TO 0
GetSprite().SetPosition(Game::SCREEN_WIDTH/2, Game::SCREEN_HEIGHT/2);
_angle = (float)sf::Randomizer::Random(0,360);
_velocity = 220.0f;
_elapsedTimeSinceStart = 0.0f;
}
//...
I figured this out, added some more conditional statements to check the boundary, if the ball hits the top then reset the ball to the middle of the screen.

CCSprite anchorPoint not working as expected?

Let me start by explaining what I am trying to do. I have a full screen animation with about 73 frames and with these images being so large I am not able to use sprite sheets so I am just adding them to an animation as seperate spriteframes. My goal is to have the animation play through and then have it disappear from left to right. The way that I am achieving this look is by updating the width of the (textureRect) of the frames and eventually making the textureRect width 0. So I have set up the CCSprite that I am going to run the animation on.
-(id) init {
if( (self=[super init])) {
self.transSprite = [CCSprite spriteWithFile:#"transition0.pvr.ccz"];
if (CC_CONTENT_SCALE_FACTOR() == 1) {
//iPhone 3Gs
self.transSprite.scaleX = .563;
self.transSprite.scaleY = .665;
self.transSprite.position = ccp(self.transSprite.contentSize.height - 3, -1);
self.transSprite.anchorPoint = ccp(1, 0);
[self addChild:self.transSprite z:5];
}
else if (kiPhone5) {
//iPhone 5
self.transSprite.scale = self.transSprite.scale * 1.335f;
self.transSprite.position = ccp(569, -1);
self.transSprite.anchorPoint = ccp(1, 0);
[self addChild:self.transSprite z:5];
}
else {
//iPhone 4
self.transSprite.scaleX = 1.126;
self.transSprite.scaleY = 1.33;
self.transSprite.position = ccp(481, -1);
self.transSprite.anchorPoint = ccp(1, 0);
[self addChild:self.transSprite z:5];
}
}
return self;
}
So the reason for checking what device is running, is because I did not make 2 versions of the animation frames HD and SD. Instead I just made them a good in between size and then I check to see what device is running and scale the sprite accordingly. So here is my problem I am setting the Sprites anchorPoint to the bottom right hand corner of the screen so that when I change the width of the textureRect it will decrease in size from left to right. Everything seems to be working great with this idea except for when I change the width of the textureRect of each sprite frame.
- (void) rectUpdate3Gs {
for (CCAnimationFrame *frame in transition.frames) {
frame.spriteFrame.rect = CGRectMake(0, 0, 856, 484);
}
}
- (void) retinaRectUpdate {
for (CCAnimationFrame *frame in transition.frames) {
frame.spriteFrame. rect = CGRectMake(0, 0, 428, 242);
}
}
So when I run the animation and start to change the width of the textureRect it decreases from both the left and the right side, it is like the anchorPoint is being ignored. Do the spriteFrames have there own anchorPoint or what is happening.
Here is an illustration of what is happening.This is the CCSprite set up for the full texture and with the anchorPoint in the bottom right corner.
self.transSprite.scaleX = .563;
self.transSprite.scaleY = .665;
self.transSprite.position = ccp(self.transSprite.contentSize.height - 3, -1);
[self addChild:self.transSprite z:5];
[self.transSprite setTextureRect:CGRectMake(0, 0, 856, 484)];
self.transSprite.anchorPoint = ccp(1, 0);
Now this is setting the textureRect of the CCSprite a little smaller.
This is the way that I want it to work, is to subtract the width of the texture from the left to right. But when I change the width of the animation frames to match this subtracted width this is what I get.
I am at a loss as to why this is happening. Do the animation frames not respect the anchor point of the CCSprite they are running on?
Scaling issue aside, there's 2 things you can do. First, and probably easiest, is to simply add a CCLayerColor on top of the animation and scale that down accordingly. This way you don't need to modify every animation frame's rect.
Second option would be to use a clipping node, found here:
Here

Cocos2d Box2d Scaling Radius Circle Bodies

Currently, in cocos2d, I have a an app that does the following:
Initiate with a Blank Screen.
When I tap the screen, I get a circle to pop-up. As I hold the circle, the circle will continue to grow at a constant rate. However, despite the fact that the sprite is growing, the box2d physical body isn't, which means that the sprite will not collide with other bodies. I been trying to figure out a way to change the radius that scales with the sprite but no such question exist here for cocos2d. I have noticed other box2d for things other than cocos2d but I am having a hard time translating them over.
//smile.position = ccp(touchLocation.x, touchLocation.y);
smile.scale = .05;
[self addChild:smile];
// b2BodyDef smileBodyDef;
smileBodyDef.type = b2_dynamicBody;
smileBodyDef.position.Set(touchLocation.x/PTM_RATIO, touchLocation.y/PTM_RATIO);
smileBodyDef.userData = smile;
smileBody = world->CreateBody(&smileBodyDef);
//Radius
b2CircleShape smileCircleShape;
int radius = 80;
//Fixture
smileFixtureDef.shape = &smileCircleShape;
smileFixtureDef.density = 0.00f;
smileFixtureDef.friction = .2f;
smileBody->CreateFixture(&smileFixtureDef);
if (CGRectContainsPoint(smileRect, touchLocation)) {
growForever = [CCRepeatForever actionWithAction: [CCScaleBy actionWithDuration: .5 scale: 1.2]];
[growForever setTag:1];
[smile runAction:growForever];
Each time you want to change your radius, grab the shape object associated with the b2Fixture that you created for your body, and then set the new value accordingly:
fixture->GetShape()->m_radius = new_radius/PTM_RATIO;

Shooting bullets with a joystick cocos2d

I have a working joystick in my cocos2d app but I cannot figure out how to make the 'player' shoot bullets out of it in the direction the joystick is pointing. I have the player moving and rotating. Also the bullets need to disappear when they hit the edges of the screen. Any help would be great. Thanks in advance.
You should get the angle from joystick.
For instance, SneakyInput has a degrees property which enables you to rotate your bullets like this :
_bullet.rotation = -joystick.degrees;
And your update method can be like this :
void update:(ccTime) delta
{
float moveAngle = _bullet.rotation;
CGPoint deltaPos = CGPointMake(cos(moveAngle) * velocity, sin(moveAngle) * velocity);
_bullet.position = ccpAdd(self.position, deltaPos);
}