i have been looking around the Internet to find out how to spawn Enemy Sprites at rondom, in a random place, I'm making a game that involves a sprite jumping an it has to dodge these 'Enemy' sprites or objects blocking it's path.
If anybody has any idea on how to do this could you please help me? Thanks in advance!
Alfie
You use objective-c's random number functions to create a random position...
sprite.position = ccp( arc4random_uniform( 800 ), arc4random_uniform( 600 ) );
Related
As the picture shows.
How can I make part A stick intensely with part B ? to make a tank body ?
contex shape is OK but cancerve is not . so I want to make two parts stick together and move....
Thanks in advance. PS I am using cocos2d v3 , it is chipmunk encapsulated with cocos2d, no Box2D here..
=============
Now I have resolved the issue . use Shapes list can do . But new questions comes : how can I put the gun on tank ? pivot body can rotate some angle but not 360.
there are only 4 connects in CCPhysicsJoint:
+(CCPhysicsJoint *)connectedPivotJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB anchorA:(CGPoint)anchorA;
+(CCPhysicsJoint *)connectedDistanceJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
anchorA:(CGPoint)anchorA anchorB:(CGPoint)anchorB;
+(CCPhysicsJoint *)connectedDistanceJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
anchorA:(CGPoint)anchorA anchorB:(CGPoint)anchorB
minDistance:(CGFloat)min maxDistance:(CGFloat)max;
+(CCPhysicsJoint *)connectedSpringJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
anchorA:(CGPoint)anchorA anchorB:(CGPoint)anchorB
restLength:(CGFloat)restLength stiffness:(CGFloat)stiffness damping:(CGFloat)damping;
no one fit.
And my question is , can I rotate a angle on a shape in shape-arranged body ?
You can create a body with multiple shapes attached.
http://www.cocos2d-iphone.org/docs/api/Classes/CCPhysicsBody.html#//api/name/bodyWithShapes:
I have one issue regarding box2d and cocos2d. My world have zero gravity and i am working in tile base game. I am using sneak joystick for movement of sprite and its move perfect but when i release point to joystick my sprite body can not stop because of some force. I want to stop that movement of sprite when joystick release.
-(void)update:(ccTime)dt :(b2Body *)ballBody :(CCSprite *)player
{
CGPoint scaledVelocity=ccpMult(joysticks.velocity, 2);
NSLog(#"Joystick Velocity X: %f",joysticks.velocity.x);
NSLog(#"Joystick Velocity Y: %f",joysticks.velocity.y);
b2Vec2 force=b2Vec2(scaledVelocity.x/PTM_RATIO,scaledVelocity.y/PTM_RATIO);
ballBody->ApplyLinearImpulse(force, ballBody->GetWorldCenter());
}
Here scaledVelocity value is approximate 0 to 1. When i release joystick that time value of joystick is 0.0
Please help me i am stuck since last 5 days.
Please help me.
Thanks in advance
Do you want the b2Body to immediately stop or to slow down (and eventually stop)?
To make it stop immediately:
ballBody->SetLinearVelocity(b2Vec2(0,0));
To make it slow down:
ballBody->SetLinearDamping(10.0); // experiment with the damping factor value until you get the right deceleration
You should check out the answer to this question:
How to stop the forces acting on a body in box2d
On release of the joystick, you should reset the velocity of your box2d body.
I have a CCSprite object of which I need to update the on screen (x,y) position as quickly as possible. It is an augmented reality app so the on screen position needs to appear fixed to a real world location.
Currently, during each update I check the heading and attitude of the device then move the sprite accordingly by determining the new x and y positions
[spriteObject setPosition:ccp(newX, newY)];
Each degree change in heading corresponds to 10 pixels in on screen position, so by setting the position this way the sprite jumps around in intervals of 10 pixels which looks stupid. I'd like to animate it smoothly, maybe by using
[spriteObject runAction:[CCMoveTo actionWithDuration:0.2f position:ccp(newX, newY)]];
but the problem here is that a new position update comes in while the sprite is animating and it sort of screws the whole thing up. Anyone know of a nice solution to this problem? Any help is much appreciated as I've tried numerous failed solutions to this point.
You can try to just animate your sprite movement to the point. I mean, you can several times during one second run animated position correction with duration of 1/numberOfUpdates in one second. Something like
- (void) onEnter
{
[super onEnter];
[self schedule:#selector(updatePositionAnimated) interval:0.2f];
}
- (void) updatePositionAnimated
{
[spriteObject runAction:[CCMoveTo actionWithDuration:0.2f position:ccp(newX, newY)]];
}
I suppose, you will have smooth enough animation in this case
using C++ and allegro 5,
I'm doing a maze-type game and wanted to find out the best way of creating the maze ??
is it simply a case of putting down a bunch of rectangles/squares ??
How would you do collision detection once you have a maze (stop player from passing through walls) ??
i'm o.k with bounding box collision detection between 2 objects but i cant think of what to do with a whole maze.
(note, i've just recently started learning allegro)
any advice appreciated.
I am new at this as well, but creating a bitmap inside allegro and then drawing your maze to the bitmap.
ALLEGRO_BITMAP *maze = NULL;
al_set_target_bitmap(maze);
al_draw_filled_square(x,y,x,y);
al_draw_filled_rectangle(x,y,x,y);
since you change target to maze, all the drawing done after will be in the maze bitmap.
then you can just draw maze to the screen and it will have all your squares and rectangles in it.
just reset you target to the display after drawing you maze.
al_set_target_bitmap(al_get_backbuffer(display));
Hi i want to develop game like 'Doodle jump'.But i have some problem with the following features-
1.How to move background scene/image.
2.How to detect collision between object.Is it needed a physics engine like box2d or i should just use manual collision.
3.what should be the size of the background image.
4.In fact i have no idea how does background move .So i need a explanation from someone.
Background Movement
A) You could create a TMX Tilemap and then make a very high Tiled-Map.
B) You could create one texture and then cycle the texture coords instead of really moving it.
Detect it manually. Best is detect it via "Point in Boundingbox" or "Rect in Rect".
For more detail visit my blog entry for collision detection with cocos2d : http://www.anima-entertainment.de/?p=262
Size of an Image
Keep in Mind that textures are always at power of 2 in the memory. If you want to create one Background-Image at retina highresolution (960x640 Pixel) in the memory will be a texture of 1024x1024. If possible use smaller Background-Images and stretch them. (like 512x512). But I really would recommend for big scrolling images the TMX Support.
CCTMXTiledMap * tmxNode = [CCTMXTiledMap tiledMapWithGMXFile:#"Level.tmx"];
// lets say you want to move it 50 pixels down in 1 second :
[tmxNode runAction:[CCMoveBy actionWithDuration:1.0 position:ccp(0,-50)];
To create a tilemap : http://www.mapeditor.org/
In the folder of cocos2d, you could get many demos of tilemap.
TileMapTest.h
TileMapTest.m
refer this tutorial this will helpful for you.
http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d
this is used screen movement with pan recognizer