Touch event Cocos2d iPhone - ios

I have doubts about touch event in cocos2d, in particular:
I have two layers:
GameLayer.m
-(id)init
{
if(self=[super init])
{
NSLog(#"GAME");
CCSprite*game=[CCSprite spriteWithFile:#"alien.png"];
CGSize size=[CCDirector sharedDirector].winSize;
self.contentSize=CGSizeMake(50,50);
self.touchEnabled=YES;
game.position=CGPointMake(40,40);
[self addChild:game];
//NSLog(#"%d",[[self children] count]);
//NSLog(#"%f",self.position.x);
}
return self;
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"GAME");
}
UserInterfaceLayer.m
-(id)init
{
if(self=[super init])
{
NSLog(#"USER");
self.touchEnabled=YES;
CCSprite*game=[CCSprite spriteWithFile:#"spiders.png"];
CGSize size=[CCDirector sharedDirector].winSize;
self.contentSize=CGSizeMake(20,20);
game.position=CGPointMake(40,40);
[self addChild:game];
//NSLog(#"%d",[[self children] count]);
//NSLog(#"%f",self.position.x);
}
return self;
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"USER");
}
Both are children of one scene:
MultiLayerScene.m
-(id)init
{
if(self=[super init])
{
sharedMultiLayerScene = self;
self.contentSize=CGSizeMake(30, 30);
CGSize size=[CCDirector sharedDirector].winSize;
// The GameLayer will be moved, rotated and scaled independently
GameLayer* gameLayer = [GameLayer node];
gameLayer.position=CGPointMake(250,250);
[self addChild:gameLayer z:1 tag:1];
//gamelayerposition = gameLayer.position;
// The UserInterfaceLayer remains static and relative to the screen area.
UserInterfaceLayer* uiLayer = [UserInterfaceLayer node];
uiLayer.position=CGPointMake(-100, -100);
[self addChild:uiLayer z:-1 tag:2];
}
return self;
}
My problem is that I touch on screen at any position their ccTouchesBegan event run.
For example if I touch only on GameLayer runs both the GameLayer touches event and the other.
I try also to insert the two layer in two different position(as you can see from the code) , but the problem still persist.
How Do I resolve this problem?
I would like ,for example,if I touch on Gamelayer respond only its touch event.

Try this
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
Priority: lower number = higher priority.

Related

How can I make sprite universal in Cocos2d iOS for garbage cleanup?

I'm currently learning Cocos2D and I can't figure out how to access my sprite '_imgStudio' to make it universal so I can clean it up the image after my "removeStudio" timer finishes. I'd like to be able to move both the CCSprite draw functions into the spawnStudio function and to kill it once the timer finishes running.
However, I get an error saying 'Use of undeclared identifier '_imgStudio' so my removeStudio timer right now is pretty much useless.
Here's part of my helloWorldFile.
#implementation GameEngine
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
GameEngine *layer = [GameEngine node];
[scene addChild: layer];
return scene;
}
#pragma mark Main Initializer
//Initalizes GameSetup
-(id) init
{
if( (self=[super init]) ) {
NSLog(#"Game Setup Worked!");
CGSize winSize = [CCDirector sharedDirector].winSize;
CCSprite *_imgStudio = [CCSprite spriteWithFile:#"Studio~ipad.png"];
_imgStudio.opacity = 0;
_imgStudio.position = ccp(winSize.width/2, winSize.height/2);
[_imgStudio runAction:[CCFadeTo actionWithDuration:2.5f opacity:255]];
[self addChild:_imgStudio];
[self scheduleOnce: #selector(spawnStudio:) delay:10];
[self scheduleOnce: #selector(removeStudio:) delay:20];
}
return self;
}
-(void) spawnStudio: (ccTime) dt {
NSLog(#"TEST");
}
-(void) removeStudio: (ccTime) dt {
[_imgStudio removeFromParentAndCleanup:YES];
NSLog(#"ImageRemoved");
}
Thanks!

Move 2 CCSprite with UITouch

I try make a simple game in cocos2d at now i have something like that
#import "GameplayLayer.h"
#implementation GameplayLayer
-(id)init {
self = [super init];
if (self != nil) {
CGSize screenSize = [CCDirector sharedDirector].winSize;
// właczenie obsługi dotyku
self.isTouchEnabled = YES;
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
}
else{
paddle1 = [CCSprite spriteWithFile:#"bijakiPhone.png"];
paddle2 = [CCSprite spriteWithFile:#"bijakiPhone.png"];
puck = [CCSprite spriteWithFile:#"krazekiPhone.png"];
}
//Polozenie i inicjalizacja paletki nr 1
[paddle1 setPosition:
CGPointMake(screenSize.width/2,
screenSize.height*0.17f)];
[self addChild:paddle1];
//Polozenie i inicjalizacja paletki nr 2
[paddle2 setPosition:
CGPointMake(screenSize.width/2,
screenSize.height*0.83f)];
[self addChild:paddle2];
//Polozenie i inicjalizacja krązka
[puck setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)];
[self addChild:puck];
}
return self;
}
//onEnter
- (void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[super onEnter];
}
//onExit
- (void)onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}
-(BOOL)containsTouch:(UITouch *)touch {
CGRect r=[paddle1 textureRect];
CGPoint p=[paddle1 convertTouchToNodeSpace:touch];
return CGRectContainsPoint(r, p );
}
-(BOOL)containsTouch2:(UITouch *)touch {
CGRect r=[paddle2 textureRect];
CGPoint p=[paddle2 convertTouchToNodeSpace:touch];
return CGRectContainsPoint(r, p );
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if ([self containsTouch:touch]){
CCLOG(#"krarzek 1 tapniety");
isTouched1 = YES;
}
if ([self containsTouch2:touch]){
CCLOG(#"krarzek 2 tapniety");
isTouched2 = YES;
}
return YES;
}
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
if (isTouched1){
CGPoint newTouchLocation = [touch locationInView:touch.view];
newTouchLocation = [[CCDirector sharedDirector] convertToGL:newTouchLocation];
[paddle1 setPosition:newTouchLocation];
}
if (isTouched2){
CGPoint newTouchLocation = [touch locationInView:touch.view];
newTouchLocation = [[CCDirector sharedDirector] convertToGL:newTouchLocation];
[paddle2 setPosition:newTouchLocation];
}
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
if (isTouched1){
isTouched1=NO;
CCLOG(#"krarzek 1 zwolniony");
}
if (isTouched2){
isTouched2=NO;
CCLOG(#"krarzek 2 zwolniony");
}
}
#end
Works CCSprite move, but when i touch 2 CCSprite at same time, They overlap itself!
How i can move them separately?
Sorry for my English and Thanks for help!
The reason of your problem that you don't store somewhere what touch is connected to your paddle. So in case of both touches are inside your paddle sprites, both isTouched1 and isTouched2 variables have value YES. So in your ccTouchMoved:withEvent: method both sprites will be placed to the same position in this case. Store your touches in some variable or I suggest to use dictionary for this with touch as a key and sprite that you need to move as value. In this case your ccTouchMoved:withEvent: method will be look like this
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint newTouchLocation = [touch locationInView:touch.view];
newTouchLocation = [[CCDirector sharedDirector] convertToGL:newTouchLocation];
CCNode paddle = [_yourDict objectForKey: touch];
[paddle setPosition:newTouchLocation];
}
And names of your methods that determine if sprite contains given touch are not good enough. I could not say what they do without looking through the code.

Cocos2d Drawing App Making Lines

I am making a very simple drawing application. I got the lines to draw using the ccTouchMoved event. I am putting all the touch moved moved points into an array and then using a for loop to draw a line between all the points. Now, I do not want to join the points when I have lifted my finger and started new line drawing. I got that part working too but now whenever I begin a new drawing the whole screen flicker.
//
// HelloWorldLayer.mm
// DrawPuppets
//
// Created by Mohammad Azam on 12/11/12.
// Copyright __MyCompanyName__ 2012. All rights reserved.
//
// Import the interfaces
#import "DrawPuppetLayer.h"
#import "AppDelegate.h"
#import "PhysicsSprite.h"
enum {
kTagParentNode = 1,
};
#pragma mark - HelloWorldLayer
#interface DrawPuppetLayer()
-(void) initPhysics;
-(void) addNewSpriteAtPosition:(CGPoint)p;
-(void) createMenu;
#end
#implementation DrawPuppetLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
DrawPuppetLayer *layer = [DrawPuppetLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init
{
if( (self=[super init])) {
// enable events
self.isTouchEnabled = YES;
self.isAccelerometerEnabled = YES;
index = -1;
canvas = [[NSMutableArray alloc] init];
// init physics
[self initPhysics];
[self scheduleUpdate];
}
return self;
}
-(void) draw
{
if([lineDrawing.points count] > 1)
{
for(int i = 0; i<([canvas count]) ;i++)
{
LineDrawing *drawing = (LineDrawing *) [canvas objectAtIndex:i];
for(int j=0;j<[drawing.points count] - 1;j++)
{
LinePoint *firstPoint = (LinePoint *) drawing.points[j];
LinePoint *secondPoint = (LinePoint *) drawing.points[j + 1];
CGPoint point1 = [[CCDirector sharedDirector] convertToGL:CGPointMake(firstPoint.x, firstPoint.y)];
CGPoint point2 = [[CCDirector sharedDirector] convertToGL:CGPointMake(secondPoint.x, secondPoint.y)];
ccDrawLine(point1, point2);
}
}
}
//
// IMPORTANT:
// This is only for debug purposes
// It is recommend to disable it
//
[super draw];
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
kmGLPushMatrix();
world->DrawDebugData();
kmGLPopMatrix();
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
lineDrawing = [[LineDrawing alloc] init];
lineDrawing.points = [[NSMutableArray alloc] init];
[canvas addObject:lineDrawing];
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: [touch view]];
LinePoint *linePoint = [[LinePoint alloc] init];
linePoint.x = point.x;
linePoint.y = point.y;
[lineDrawing.points addObject:linePoint];
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//Add a new body/atlas sprite at the touched location
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
}
}
#end
Can anyone spot my mistake?
Try visiting it all down to a texture, there's going to be a time when your points array gets too large to be drawn nicely
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get the node space location of our touch
CGPoint location = [self getNodeSpaceTouchLocationFromUIEvent:event];
// draw with our current location and a random colour
[_canvas begin]; // our rendertexture instance
// do your drawing here
[_pen drawPenWithPosition:location andColour:_colour];
// end capturing the current pen state
[_canvas end];
}
Here's a simple example project written for iOSDevUK 2012 it uses GL_POINTS in Cocos2d v1 and is based on the approach we took when developing SketchShare

Touches in Cocos2d, some bug

I've written the following program, which has to do the following: when user touch moving sprite it has to be removed from the scene.
But, when I run my code the following thing happens: when I touch the uppest sprite it dissappears whith it neighbour. How can I fix it?
Here is the code.
UPD: I've tested this code on smaller *.png file, and everything works fine. But on bigger *.png file (smth like 200x200 pixels, iPhone simulator) I have a bug: object removes on touchEvent with its the nearest neighbour.
definition
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#interface GameplayLayer : CCLayer {
NSMutableArray *arrayOfSprites;
}
#end
#import "GameplayLayer.h"
#implementation GameplayLayer
-(id)init
{
self = [super init];
self.isTouchEnabled=YES;
arrayOfSprites=[[NSMutableArray alloc] init];
if (self != nil)
{
int j=100;
for (int i=0; i<=2; i++) {
[arrayOfSprites addObject:[CCSprite spriteWithFile:#"sv_anim_1-hd.png"]];
[[arrayOfSprites objectAtIndex:i] setPosition:CGPointMake(j,j)];
[self addChild:[arrayOfSprites objectAtIndex:i] z:0 tag:i];
j+=100;
}
[self startRunning];
}
return self;
}
-(void)startRunning
{
CGSize screenSize=[[CCDirector sharedDirector] winSize];
for ( CCSprite * currentSprite in arrayOfSprites)
{
[currentSprite runAction:[CCMoveTo actionWithDuration:10 position:CGPointMake(screenSize.height,screenSize.width/2)]];
}
}
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0
swallowsTouches:YES];
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint locationOfTouch=[self convertTouchToNodeSpace: touch];
for (CCSprite *currentSprite in arrayOfSprites)
{
if (CGRectContainsPoint(currentSprite.boundingBox, locationOfTouch))
{
NSLog(#"Sprite was touched");
[self removeChild:currentSprite cleanup:YES];
}
}
}
#end
Try this function:
-(CGRect)getSpriteRect:(CCNode *)inSprite
{
CGRect sprRect = CGRectMake(
inSprite.position.x - inSprite.contentSize.width*inSprite.anchorPoint.x,
inSprite.position.y - inSprite.contentSize.height*inSprite.anchorPoint.y,
inSprite.contentSize.width,
inSprite.contentSize.height
);
return sprRect;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
for (CCSprite *currentSprite in arrayOfSprites)
{
CGRect rect = [self getSpriteRect:currentSprite];
if (CGRectContainsPoint(rect, location))
{
NSLog(#"Sprite was touched");
[self removeChild:currentSprite cleanup:YES];
}
}
}

Touch location CCLayer

*Working Code Now*
OK, I thought that this would be easy to get working but it turns out to not work like I expected.
I am trying to get the Touch location from a CCLayer that can be moved or zoomed, not the location on the screen itself? here is how I thought that it would work but it crashes?
Interface
#import "cocos2d.h"
#interface TestTouch : CCLayer {
CCLayerColor *layer;
}
+(CCScene *) scene;
#end
Implementation
#import "TestTouch.h"
#implementation TestTouch
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
TestTouch *layer = [TestTouch node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
- (id)init
{
self = [super init];
if (self) {
CGSize winsize = [[CCDirector sharedDirector]winSize];
CCLayerColor *layer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)];
// layer.scale = 0.7f;
layer.contentSize = CGSizeMake(640, 960);
layer.position = CGPointMake(winsize.width/2, winsize.height/2);
layer.isRelativeAnchorPoint = YES;
[self addChild:layer z:0];
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:layer priority:0 swallowsTouches:YES];
}
return self;
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchStart = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
touchStart = [layer convertToNodeSpace:touchStart];
NSLog(#"Touch:%f,%f",touchStart.x, touchStart.y);
return YES;
}
#end
If I change this line to include "self":
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
It will obviously work but then I get the location relevant to the screen and not the layer, which is what I need.
You need to convert the location on the screen to the layer's "node space":
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchStart = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
// convert touch location to layer space
touchStart = [self convertToNodeSpace:touchStart];
NSLog(#"Touch:%f,%f",touchStart.x, touchStart.y);
return YES;
}

Resources