How can i shoot the sprite "ink" straight up? The player Squiddy moves across the screen and i would like it to shoot the ink up, straight up, everytime. The code i have so far shoots the ink but it shoots it to a certain point on the screen
The code
CGPoint targetPosition = ccp(self.contentSize.width/2, self.contentSize.height/2 + self.contentSize.height);
ink = [CCSprite spriteWithImageNamed:#"MarioTube_Body_Rev.png"];
ink.position = Squiddy.position;
ink.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:ink.contentSize.width/2.0f andCenter:ink.anchorPointInPoints];
ink.physicsBody.collisionGroup = #"playerG roup";
ink.physicsBody.collisionType = #"projectileCollision";
CCActionRotateBy *actionSpin = [CCActionRotateBy actionWithDuration:.5f angle:360];
[ink runAction:[CCActionRepeatForever actionWithAction:actionSpin]];
[_physicsWorld addChild:ink];
CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:.75f position:targetPosition];
CCActionRemove *actionRemove = [CCActionRemove action];
[ink runAction:[CCActionSequence actionWithArray:#[actionMove,actionRemove]]];
As I said before, try to apply a force or impulse to ink.physicsBody:
CGPoint force = ccpMult(ccp(0,90), 600); //x=0, y=90, force=600
[ink.physicsBody applyForce:force];
With a value of x=0, the force will go up. Just play with the values.
Hope this helps
Related
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!
I am using the following code to make the camera follow my character as he falls down the screen:
[self runAction:[CCFollow actionWithTarget:charSprite worldBoundary:[levelLoader gameWorldSize]/*CGRectMake(0,0,320,3000)*/]];
What happens is this: the code runs as you would expect, and follows the character as he falls from the top of the screen. However: when he reaches the middle of the screen the camera stops following him.
I have tried both the code above as well as replacing the [levelLoader gameWorldSize] with the commented out CGRectMake(0,0,320,3000) - both yield the same results. As a last resort I tried to use some code I found on www.raywenderlich.com that has worked for me before (see below) (it worked on the x-axis in another game I wrote), but the same problem occurred.
-(void)setViewpointCenter:(CGPoint) position
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
CGRect worldRect = [levelLoader gameWorldSize];
int x = MAX(position.x, worldRect.origin.x + winSize.width / 2);
int y = MAX(position.y, worldRect.origin.y + winSize.height / 2);
x = MIN(x, (worldRect.origin.x + worldRect.size.width) - winSize.width / 2);
y = MIN(y, (worldRect.origin.y + worldRect.size.height) - winSize.height/2);
CGPoint actualPosition = ccp(x, y);
CGPoint centerOfView = ccp(winSize.width/2, winSize.height/2);
CGPoint viewPoint = ccpSub(centerOfView, actualPosition);
self.position = viewPoint;
}
My world is created in Portrait mode using LevelHelper.
Any help would be greatly appreciated!
CCFollow follows the node within the world boundary, minus half the screen width/height. If your sprite starts falling at y = 320 then CCFollow will follow it until y = 160.
You can either position the sprite to your world boundary's top (y = 3000) or make the world boundary negative (y = -3000). Either way, when the sprite reaches the world boundary, scrolling will stop half a screen width/height before the world boundary.
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;
I have been having trouble with CCLayerPanZoom for weeks now and finally got close but am still having an issue. I have a tile map that is quite large 8000 pixels x 8000 pixels and what I'd like to to is have the ability to zoom in to about 2.0f and zoom out to about 0.4f. The below code works great in that it lets me pan around my entire tile map and NOT pan past the edges - a common problem with CCLayerPanZoom, however the code will not allow zoom in or out. I have commented out minScale and maxScale for now since neither is working. I have tried changing the location of minScale and maxScale and it doesn't work anywhere. Does anyone have any ideas how to get minScale and maxScale to work so zooming will function?
//PanZoomLayer
_panZoomLayer = [[CCLayerPanZoom node] retain];
[self addChild: _panZoomLayer];
_panZoomLayer.delegate = self;
[_panZoomLayer addChild: _tileMap z :1 tag: kBackgroundTag];
_panZoomLayer.mode = kCCLayerPanZoomModeSheet;
_panZoomLayer.rubberEffectRatio = 0.0f;
CCNode *backgroundZ = [_panZoomLayer getChildByTag: kBackgroundTag];
CGRect boundingRect = CGRectMake(0, 0, 0, 0);
boundingRect.size = [backgroundZ boundingBox].size;
[_panZoomLayer setContentSize: boundingRect.size];
_panZoomLayer.anchorPoint = ccp(0.5f, 0.5f);
_panZoomLayer.position = ccp(0.5f * winSize.width, 0.5f * winSize.height);
_panZoomLayer.panBoundsRect = CGRectMake(0, 0, winSize.width, winSize.height);
_panZoomLayer.minScale = 0.4f;
_panZoomLayer.maxScale = 2.0f;
//end PanZoomLayer
Finally figured it out, by adding the below line to the code above the CCPanZoomLayer finally works great. Hopefully this code hopes others out that have struggled with this cocos2d extension
[[[CCDirector sharedDirector] view] setMultipleTouchEnabled:YES];
Hi I have finally made a working joystick in cocos2d. I am able to rotate a sprite to the exact angle that the joystick thumb, or cap, is 'pointing'. However, I am unable to move the sprite in that same direction. Is there an easy way to move the sprite with the way I have the rotating code set up? Also is there a way to keep it moving if your thumb is still pressed, but not moving the joystick?. PS this code is all within the TouchesMoved method. PPS. the cap is the thumb, the pad is the joystick background, and the Sprite2 is the sprite that I want to move. (95, 95) is the center of the pad sprite.
if(capSprite.position.x>=padSprite.position.x){
id a3 = [CCFlipX actionWithFlipX:NO];
[sprite2 runAction:a3];
}
if(capSprite.position.x<=padSprite.position.x){
id a4 = [CCFlipX actionWithFlipX:YES];
[sprite2 runAction:a4];
}
CGPoint pos1 = ccp(95, 95);
CGPoint pos2 = ccp(capSprite.position.x, capSprite.position.y);
int offX = pos2.x-pos1.x;
int offY = pos2.y-pos1.y;
float angleRadians = atanf((float)offY/(float)offX);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
float theAngle = -1 * angleDegrees;
sprite2.rotation = theAngle;
I'm not familiar with cocos2d but I had a quick look at the documentation and this sample might be of use to you:
if keys[key.UP]:
self.target.acceleration = (200 * rotation_x, 200 * rotation_y)
I had written a long explanation answering your second question but I believe this "self.target.acceleration" solves that too. You can read more at the cocos2d API documentation.
What I generally do is get the angle, convert it to a CGPoint with ccpForAngle(float) and then multiply the CGPoint by a value:
float angle = whatever;
CGPoint anglePoint = ccpForAngle(angle);
// You will need to play with the mult value
angle = ccpMult(angle, 2.5);
// This also works with box2D or probably Chipmunk.
sprite.position = angle;