I am a complete newb so please forgive if this questions totally disqualifies me from using cocos2d.
I've been messing around with sdk for a short while and cocos2d was suggested as a good starting point for creating a game. Right now I've managed to get through a bunch of tutorials.
I understand the concept of scenes, layers and sprites. In my game I have 2 sprites and I want the user to touch one and have the following action:
touched sprite should vibrate and the other sprite should transform into another image file.
Am I thinking in the right direction that a touch layer (cs object) should be created containing both the commands or two seperate layers with each command for each sprite? I would appreciate pointin me in the right direction :-)
don't know where to start with action when touching sprite
(34 posts) (7 voices)-
Posted 2 years ago #
-
First of all, NOTHING disqualifies you or anyone from using Cocos2D, as we all started out as newbies and we all learn through trial & error and through questions. Nothing wrong with that.
As for your question, I didn't quite understand - you wish to detect a touch on a sprite and then trigger two actions, one for each sprite, am I right?
If so, you can detect touches using the Bool functionCGRectContainsPoint(spriteRect, touchLocation)and if yes then you trigger some method... should look like this:-(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:[touch view]]; CGRect spriteRect = [mySprite getRect]; //implement the getRect method, if you require it just ask. if(CGRectContainsPoint(spriteRect, location)) [self doAction]; } - (void)doAction { [mySprite runAction:[Shaky3D actionWithRange:50 shakeZ:NO grid:ccg(15, 10) duration:3.0]]; [mySprite2 setTexture:[[TextureMgr sharedTextureMgr] addImage:@"spriteTransform.png"]; }Hope I got your point, and this helped...
~Natanavra.Posted 2 years ago # -
thanks :-)
I'll have to try this code.
To summarize:
I want MySprite to detect touch and vibrate and at the same time I want MySprite2 to transform into another image due to the touch of MySprite.
Am I thinking in the write direction to have this code added to another class e.g. TouchLayer which will control all touches to sprites???
:-)Posted 2 years ago # -
Hello, and welcome!
You could also check the touchestest which comes with cocos2d. There, another method is used from the one natanavra mentioned.
Using that method you can define ccTouches methods for each class instead of having to define them in your layer. For me it makes it cleaner and easier to manage if you have many touchable objects.
Posted 2 years ago # -
@pabloruiz55
OK will do :-)Posted 2 years ago # -
CGRect spriteRect = [mySprite getRect]; //implement the getRect method, if you require it just ask.Ok, i'm asking! what is the "getRect" method? :)
Posted 2 years ago # -
getRect is just a function that you should write for your object. It will return a CGRect for the collision check.
Normally you want it to have its position at the object's position and its width/height would be the objects' position + the object's size.
Notice that if you have set your object's anchor point in the middle of the sprite you would have to take that into account... your object's position would be (sprite.position.x - sprite.width/2).
Posted 2 years ago # -
is there an example of this method? I'm still learning.
Posted 2 years ago # -
Hi Osiris,
if you like to code without implimenting getRect method, you can do like this:
- (BOOL)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { UITouch* touch = [touches anyObject]; CGPoint location = [touch locationInView:[touch view]]; CGRect myRect = CGMakeRect(mySprite.position.x, mySprite.position.y, mySprite.contentSize.width, mySprite.contentSize.height); if(CGRectContainsPoint(myRect, location)) { // mySprite got touched // Your codes come here. return kEventHandled; } }Posted 2 years ago # -
thanks!
in reference to "if you like to code without implimenting getRect method, you can do like this:"....I have no idea. I don't yet know what I should be doing and what I shouldn't be. Im kinda fumbling around in the dark, making sentences from streams of syllables, hoping they might sound like words!
I am learning a lot, but every page I turn, I realize how much further I have to go.
Posted 2 years ago # -
when I try to use that...
CGRect myRect = CGMakeRect(mySprite.position.x, mySprite.position.y, mySprite.contentSize.width, mySprite.contentSize.height);I get
"warning: Implicit declaration of function 'CGMakeRect'
and
"error: invalid initializer"
and yes, i'm retrieving a real sprite...
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:[touch view]]; Sprite *mySprite = (Sprite *)[self getChildByTag:4]; CGRect spriteRect = CGMakeRect(mySprite.position.x, mySprite.position.y, mySprite.contentSize.width, mySprite.contentSize.height); if(CGRectContainsPoint(spriteRect, location)) [self spriteAction]; return kEventHandled; }Posted 2 years ago # -
Hi Osiris,
I'm sorry I wrote a wrong method.
Please replace CGMakeRect with 'CGRectMake'.
Posted 2 years ago # -
he he - I was searching the entire cocos2d Project and wondering....
Posted 2 years ago # -
YAAY! It works! Thank you again.... One more step towards World Domination!
Posted 2 years ago # -
Hi,
Good to hear that!
Posted 2 years ago # -
I'm hoping that the answers to these probably very basic questions is helping others besides myself.
Posted 2 years ago # -
two things I've noticed and don't have an explanation for.
#1 - my routine above is only recording touch detection in the upper right quadrant.
Ive tried the
CGPoint convertedPoint = [[Director sharedDirector] convertCoordinate:location];but that doesnt change anything.
and
#2 - I run an action when the screen records a touch that shakes the screen...
[bg runAction:[Shaky3D actionWithRange:50 shakeZ:NO grid:ccg(15, 10) duration:3.0]];It works, but it leaves my FPS at 20.0??
Posted 2 years ago # -
sorry, I I should have showed more relevant code. I already have:
CGPoint location = [touch locationInView:[touch view]];further up.
Posted 2 years ago # -
Hi
Original poster here!
Just wanted to clarify whether this code goes in a new touch class layer?
(Help!)Posted 2 years ago # -
I have all sprites in one layer (but i'm having some "issues" so i'm not sure if this is the best way at all).
I dropped my whole
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)eventP{}in another thread a few minutes ago, if there is something in there that might be helpful. It is mostly working.
Posted 2 years ago # -
getRect is just a function that you should write for your object. It will return a CGRect for the collision check.
Normally you want it to have its position at the object's position and its width/height would be the objects' position + the object's size.
Notice that if you have set your object's anchor point in the middle of the sprite you would have to take that into account... your object's position would be (sprite.position.x - sprite.width/2).
I've been struggling for a couple of hours now... I don't see where I should make the adjustment at between the sprite's position and the perceived touch?
I am indeed setting my position of my bg sprite to 240,160, but I dont see any other way to get it centered on the screen? If I attempt to touch it, I can only pickup the touch in the upper right quadrant. I'm assuming this is because of the caution you offered earlier?
Posted 2 years ago # -
It seems so,
try creating your cgrect at: x:-240 , y: -160
Posted 2 years ago # -
I'll try that, but doesn't that seem jury-rigged?
Posted 2 years ago # -
Sorry, what i meant is, if you are using the following:
CGRect spriteRect = CGMakeRect(mySprite.position.x, mySprite.position.y, mySprite.contentSize.width, mySprite.contentSize.height);
in your case the sprite is positioned ad (240,160) so, the rect will start there.
you should do:
CGRect spriteRect = CGMakeRect(mySprite.position.x - mySprite.contentSize.width /2, mySprite.position.y - mySprite.contentSize.height / 2, mySprite.contentSize.width, mySprite.contentSize.height);
This will give you a rect at (0,0);
In my code i set it to (-240,-160) and it works perfectly, it may have to do with the anchorpoint position, i think.
Posted 2 years ago # -
gag... I was trying that fix....
and all the sudden I got "No launchable executable present at path." when I tried to compile???
I didnt change anything! I swear!
Posted 2 years ago # -
Try cleaning the targets, change between simulator and device. Sometimes i just get those weird errors that come and go too... ¬¬
Posted 2 years ago # -
OK, I followed an internet resource and got it going again, but I have no idea how it changed.
Posted 2 years ago # -
and the change to CGMakeRect worked great. Thanks!
Posted 2 years ago # -
Hi!
I'm at my wits end with this one. It's so simple but I just can't get it to work!
I've taken all the above advice and tried to implement it.
My project consists of a scene and 2 sprites (PictureLayer and PictureLayer2)
I want to add a touch action when PicureLayer2 is touched it causes 2 things to happen:
(1) PictureLayer2 will shake
(2) PictureLayer will change textures (image will change)Below is the code of PictureLayer2:
Can someone see where my mistake is? Have I implemented the getRect method correctly? Why isn't it working?#import "PictureLayer2.h"
#import "cocos2d.h"@implementation PictureLayer2
-(id) init
{
self = [super init];
if (self != nil)
{
Sprite *pic = [Sprite spriteWithFile:@"groch.png"];
pic.position = ccp(420, 55);
[self addChild:pic];
}return self;
}-(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];CGRect myRect = CGRectMake (PictureLayer2.position.420, PictureLayer2.position.55, PictureLayer2.contentSize.50, PictureLayer2.contentSize.50);
if(CGRectContainsPoint(myRect, location)) {return kEventHandled;
}[self doAction];
}- (void)doAction {
[PictureLayer2 runAction:[Shaky3D actionWithRange:50 shakeZ:NO grid:ccg(15, 10) duration:3.0]];
[PictureLayer setTexture:[[TextureMgr sharedTextureMgr] addImage:@"groch.png"];
}@end
Please help!
Posted 2 years ago #
Reply »
You must log in to post.