These are my first Cocos2D projects, I'm trying to make a sprite jump in the same place when touched, but I can't make it response because I don't know how to set touch actions on sprites.
Here is the code :
-(void) spriteEffect
{
CCSprite *actionEffect = avatar;
id jump = [CCJumpBy actionWithDuration:1 position: ccp(0, 0) height:50 jumps:2];
id sequence = [CCSequence actions: jump, nil];
[actionEffect runAction:sequence];
return yes
}
Should I use a
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
Thanks!
Your comment that avatar is an array of sprites helps clarifying why you don't see any effects. Try to do something like:
-(void) spriteEffect
{
CCSprite *actionEffect = <get a sprite from avatar array>;
id jump = [CCJumpBy actionWithDuration:1 position: ccp(0, 0) height:50 jumps:2];
[actionEffect runAction:jump];
}
I don't know what kind of array avatar is, so I can't provide syntax for accessing its elements. If avatar is an NSArray, you can make all of your sprites jump by using:
-(void) spriteEffect
{
foreach (CCSprite* actionEffect in avatar) {
id jump = [CCJumpBy actionWithDuration:1 position: ccp(0, 0) height:50 jumps:2];
[actionEffect runAction:jump];
}
}
Related
I am developing a game on Xcode, but I ran into a problem. I'm not able to make my objectGuy
from going beyond a roof and I am unable to make my objectGuy from falling beyond the floor.
This is what I tried coding: I need help making the collision stop the jump and make the objectGuy fall back down and once it collides with the ground, to keep him there.
topGround is referring to the roof / bottomGround is referring to the ground
-(void) collision {
if (CGRectIntersectsRect(guy.frame, topGround.frame)) {
guyJump = 0;
}
if (CGRectIntersectsRect(guy.frame, bottomGround.frame)) {
guy.center = CGPointMake(guy.center.x, 261);
}
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
guyJump = 20;
[self collision];
}
-(void) guyMoving {
guy.center = CGPointMake(guy.center.x, guy.center.y - guyJump);
guyJump = guyJump - 5;
if (guyJump < -3) {
guyJump = -3;
}
}
For some reason my remove function on my CCNode is not working after the animation is run.
I am adding a CCNode on click, running a animation then running the remove function.
// loads the Coin
CCNode* coin = [CCBReader load:#"heros/coin"];
coin.name =#"coin";
// position
coin.position = ccpAdd(_touchNode.position, ccp(0, 0));
//Add to Parent
[_touchNode addChild:coin z:-1];
id action1 = [CCActionMoveTo actionWithDuration:0.7f position:ccpAdd(_touchNode.position, ccp(0, 200))];
id action2 = [CCActionMoveTo actionWithDuration:0.7f position:ccpAdd(_touchNode.position, ccp(0, 100))];
CCActionCallFunc *callAfterMoving = [CCActionCallFunc actionWithTarget:self selector:#selector(cleanupSprite:)];
[coin runAction: [CCActionSequence actions:action1, action2, callAfterMoving, nil]];
using the following delete function produces a crash error with This node does not contain the specified child
- (void) cleanupSprite:(CCNode*)inSprite
{
// call your destroy particles here
// remove the sprite
[self removeChild:inSprite cleanup:YES];
}
Using the following delete also doesn't work
- (void) cleanupSprite:(CCNode*)inSprite
{
// call your destroy particles here
// remove the sprite
[self removeChildByName:#"coin" cleanup:YES];
}
self does not contain coin, _touchNode does. Do this in your callback :
[_touchNode removeChildByName:#"coin" cleanup:YES];
Hi Im making a cocos2d game that has a sprite flying and falling, Im trying to have a first tap touch event for example when the user touches the screen for the first time it'll start the game animation and start the physics engine. Whats happening is that when the user starts the game the sprite falls down right away, can anyone give me a hand with this?
right now Im using something like this but Im not sure how to get the physics engine to wait until the user touches the screen for the first time.
CCSprite *_pixie
CCNode *_start;
BOOL *_firstTap;
CCPhysicsNode *_physicsNode;
-(void)didLoadFromCCB{
_physicsNode.collisionDelegate = self;
_firstTap = True;
}
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if(_firstTap == TRUE){
_start.visible = FALSE;
_firstTap = False;
}
//flying sounds & so on
if (!_gameOver) {
[[OALSimpleAudio sharedInstance] playEffect:MAGIC volume:0.4 pitch:1 pan:0 loop:NO];
[_pixie.physicsBody applyImpulse:ccp(0, 420.f)];
[_pixie.physicsBody applyAngularImpulse:11000.f];
_sinceTouch = 0.f;
}
}
- (void)update:(CCTime)delta {
if(_firstTap == FALSE){
float yVelocity = clampf(_pixie.physicsBody.velocity.y, -1 * MAXFLOAT, 200.f);
if ((_sinceTouch > .5f)) {
[_pixie.physicsBody applyAngularImpulse:-40000.f*delta];
}
}
}
Change
BOOL *_firstTap;
to
BOOL _firstTap; //No asterisk
And also make sure that you set _firsttap = YES in viewDidLoad functions
- (void)viewDidLoad
{
[super viewDidLoad];
_firstTap = YES;
}
Looks to me like the first touch Boolean value may not be defined until after the update code is called. You are also mixing BOOL values with bool values.
Objective-C : BOOL vs bool
Should be easy but i don't have find...
I have a lot of sprites in my world and on a moment X, i need to destroy multiples sprites.
With the code below, i can remove this sprite by tag number:
CCSprite *sprite = (CCSprite *)[self getChildByTag:TagFromMyArray];
[self removeChild:sprite cleanup:YES];
The problem is the body that remain on screen...and crash the game on the next tick.
With the code below i can find "a body":
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData();
if (sprite.tag = [[[myGrille.grille objectAtIndex:point.x] objectAtIndex:point.y]blockTag]) {
[self removeChild:sprite cleanup:YES];
world->DestroyBody(b);
}
}
}
The problem is that it remove every body on the world.
Isn't b a reference to a unique body ?
I was around this for hours and it was a small error...
Just have to replace "=" with "==" in the last if condition.
I'm developing a game for iPhone using COCOS2D.
In that, I need to draw a line when user drag his finger from a point to another. As far as my knowledge is concern I need to do this in Touches Moved method from where I can get the points.
But I don't know how to do this. Can anybody help me on this?
Kia ora. Boredom compels me to provide an answer on this topic.
Layer part (i.e. #interface GetMyTouches : CCLayer):
-(void) ccTouchesMoved:(NSSet *)inappropriateTouches withEvent:(UIEvent *)event
{
UITouch *touchMyMinge = [inappropriateTouches anyObject];
CGPoint currentTouchArea = [touchMyMinge locationInView:[touchMyminge view] ];
CGPoint lastTouchArea = [touchMyMinge previousLocationInView:[touchMyMinge view]];
// flip belly up. no one likes being entered from behind.
currentTouchArea = [[CCDirector sharedDirector] convertToGL:currentTouchArea];
lastTouchArea = [[CCDirector sharedDirector] convertToGL:lastTouchArea];
// throw to console my inappropriate touches
NSLog(#"current x=%2f,y=%2f",currentTouchArea.x, currentTouchArea.y);
NSLog(#"last x=%2f,y=%2f",lastTouchArea.x, lastTouchArea.y);
// add my touches to the naughty touch array
naughtyTouchArray addObject:NSStringFromCGPoint(currentTouchArea)];
naughtyTouchArray addObject:NSStringFromCGPoint(lastTouchArea)];
}
Node part (i.e. #interface DrawMyTouch: CCNode) :
#implementation DrawMyTouch
-(id) init
{
if( (self=[super init]))
{ }
return self;
}
-(void)draw
{
glEnable(GL_LINE_SMOOTH);
for(int i = 0; i < [naughtyTouchArray count]; i+=2)
{
start = CGPointFromString([naughtyTouchArray objectAtIndex:i]);
end = CGPointFromString([naughtyTouchArray objectAtIndex:i+1]);
ccDrawLine(start, end);
}
}
#end
Layer part II (i.e. #interface GetMyTouches : CCLayer):
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
DrawMyTouch *line = [DrawMyTouch node];
[self addChild: line];
}
Remember touching is easy. Knowing what you're doing while touching isn't rocket science.
Finally, if you don't understand anything i've posted ... take up baking. The world needs more chocolate cake producers.
Clarification:
No one learns from cut 'n paste ~ this code was never meant to work without caressing
If you fail to see the humour, you're in the wrong profession
Of note, I love a good chocolate cake. The world really does need more fantastic bakers. That's not an insult, that's encouragement.
"Look outside the square, to find the circle filled with the knowledge that makes life worth living" ~ Aenesidemus.
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *theTouch = [touches anyObject];
CGPoint touchLocation = [theTouch locationInView:[theTouch view] ];
cgfloat x = touchLocation.x;
cgfloat y= touchLocation.y;
printf("move x=%f,y=%f",x,y);
}
Try the above code. It will get the coordinate points when touches moved in iphone.
To draw line, use something like this:
-void draw
{
here is the code for line draw.
}
Update this function in update method.