I am having some physics body nodes. Sprite Kit immediately calls didbegincontact method. I want that method should be called when i release the touch so that it may perform actions when click was released instead of calling method immediately causing some action setting problems for me.
- (void)didBeginContact:(SKPhysicsContact *)contact
{ NSLog(#"%hhd", _touching);
if(_touching == NO)
return;
something here
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
_touching = YES;
NSLog(#"%hhd", _touching);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
_touching = NO;
NSLog(#"%hhd", _touching);
something here
}
Set the global Variable, ie
BOOL _touching;
When you touch / release (touches ended and began) you set that var to YES / NO;
In the didbegincontact, use something like
if(_touching == YES) {
// what I want to happen when I am touching
}
else {
// i must not be touching so do this
}
Here is the basic set up - however I think its the game logic thats the issue, maybe think of a different way to solve your problem
#interface XBLMyScene()
#property (strong, nonatomic) SKNode *world;
#property (strong, nonatomic) SKSpriteNode *ball;
#property BOOL touching;
#end
#implementation XBLMyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.world = [SKNode node];
[self addChild:self.world];
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
self.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointZero toPoint:CGPointMake(500, 0)];
self.ball = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(40, 40)];
self.ball.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, 40)];
self.ball.position = CGPointMake(200, 300);
[self.world addChild:self.ball];
self.touching = NO;
}
return self;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.touching = YES;
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.touching = NO;
}
- (void) didSimulatePhysics
{
if (self.touching) {
NSLog(#"I am touching");
}
else {
NSLog(#"I am not touching");
}
}
Related
The 'thePlay' gets presented but does not display the redColor background. It remains black. Can someone advice on what I am supposed to do.
More Clarification: All the scenes and quickMenu class are imported on each other
This method execute transition from this scene(gameScene) using a button in quickMenu class (subclass of SKNode)
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches){
if (self.gameEnded && quickMenu.touchable) {
SKNode *theNode = [quickMenu nodeAtPoint:[touch locationInNode:quickMenu]];
if ([theNode.name isEqualToString:#"theMenu"]){
SKScene *thePlay = [[SKScene alloc] initWithSize:self.size];
SKTransition *doors = [SKTransition doorsOpenHorizontalWithDuration:1.0];
[self.view presentScene:thePlay transition:doors];
}
}
}
}
this is the first method in another scene (thePlay scene),
-(void)didMoveToView:(SKView *)view
{
self.backgroundColor = [SKColor redColor];
NSLog(#"this method called");
}
I tried the init method, did not show the redColor background as well, as shown below...
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor redColor];
NSLog(#"init method called");
}
Am I missing something?
I want to run a SKAction if my SKSpriteNode Sorcerer is touched.
With my current code, whenever I touch, my SpriteNode 'stone' will move to the Sorcerer.
I could call the location of the touch with specific x and y coord., but since the 'Sorcerer' is moving, I can't do that.
How can I fix this?
- (void)Sorcerer {
Sorcerer = [SKSpriteNode spriteNodeWithImageNamed:#"Sorcerer.png"];
Sorcerer.size = CGSizeMake(85, 85);
Sorcerer.zPosition = 2;
Sorcerer.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(50, 50)];
Sorcerer.physicsBody.dynamic = NO;
Sorcerer.physicsBody.allowsRotation = NO;
Sorcerer.physicsBody.usesPreciseCollisionDetection = YES;
Sorcerer.physicsBody.restitution = 0;
Sorcerer.position = CGPointMake(self.frame.size.width * 1.25, self.frame.size.height / 2.2);
SKAction * actionMove = [SKAction moveToX:self.frame.size.width / 2 duration:10];
[Sorcerer runAction:[SKAction repeatActionForever:[SKAction sequence:#[actionMove]]]];
[self addChild:Sorcerer];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:Sorcerer];
Stone = [SKSpriteNode spriteNodeWithImageNamed:#"Stone.png"];
Stone.position = Human.position;
Stone.zPosition = 1;
Stone.scale = 0.6;
SKAction *action = [SKAction moveTo:Sorcerer.position duration:0.5];
SKAction *remove = [SKAction removeFromParent];
[Stone runAction:[SKAction sequence:#[action,remove]]];
[self addChild:Stone];
}
There are many situations when SKNode needs to inform its scene about some events or wants to force particular behavior only scene can provide.
Of course you can implement a touchesEnded: method in the scene and iterate through node but what if you have couple of thousands of them on the screen? The cleanest possible way of doing this is either by using delegation or notifications.
Delegation
We will create a protocol and set a scene as its delegate. Sourcerer will call delegate's method when it's touched. Let's define the protocol:
#class Sorcerer;
#protocol SorcererInteractionDelegate <NSObject>
- (void)sorcererTapped:(Sorcerer)sorcerer;
#end
Add a delegate property to Sorcerer class:
#interface Sorcerer : SKSpriteNode
#property (nonatomic, weak) id <SorcererInteractionDelegate> delegate;
#end
Finally, implement the touchesEnded: method.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
if ([_delegate respondsToSelector:#selector(sorcererTapped:)])
{
[_delegate performSelector:#selector(sorcererTapped:)
withObject:self];
}
}
Remember to set userInteractionEnabled to YES in your init method of Sourcerer class.
When creating a sourcerer in your scene, set the delegate:
Sourcerer *sourcerer = ...
sourcerer.delegate = self;
and implement the sorcererTapped: method:
- (void)sorcererTapped:(Sorcerer)sorcerer;
{
// Do stuff with sorcerer.
}
Notifications
Delegation is cool but what if you need to inform several objects at once about the change. Then, you should use notifications.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[[NSNotificationCenter defaultCenter]
postNotificationName:#"UserDidTapSourcererNotification" object:self];
}
Your scene needs to register as an observer of #"UserDidTapSourcererNotification" notification and handle it.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleUserDidTapSourcererNotification:)
name:#"UserDidTapSourcererNotification";
object:nil];
- (void)handleUserDidTapSourcererNotification:(NSNotification *)notification
{
Sorcerer *sorcerer = (Sorcerer *)[notification object];
// Do stuff with sorcerer.
}
Hope it helps.
//give stone or Sorcere a name
through this name you can easily access that node
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
// decide if the node is of interest or can be ignored, something like this:
if ([node.name isEqualToString:#“stone”]) {
//apply action on stone
}
}
I'm working on a project where images are spawned and you basically touch them and they are removed. How would I removed the object that I touched? I've thought of making a mutablearray to hold all the objects but i can't seem to figure anything out.
GameViewController.m
#import "GameViewController.h"
#import "Cig.h"
#interface GameViewController ()
#end
#implementation GameViewController
#synthesize scoreLbl, timeLbl;
//CLASS ONLY VARS
BOOL isGameOver;
int timeInt;
int scoreInt;
int cigsOnScreen;
NSMutableArray *spawnedCigs;
//CLASS ONLY VARS
//TIMER
-(void)count {
timeInt--;
timeLbl.text = [NSString stringWithFormat:#"Time: %i", timeInt];
if(timeInt == 0){
isGameOver = YES;
NSLog(#"Your Score For This Round: %i", scoreInt);
}
if(isGameOver == NO){
[self performSelector:#selector(count) withObject:nil afterDelay:1];
}
}
//TIMER
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)spawnCigs {
for(int i =0 ; i < 5; i++){
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(arc4random()%760, arc4random()%430, 100, 23)];
UIImage *image = [UIImage imageNamed:#"Cig.png"];
[imageView setImage:image];
Cig *cig = [[Cig alloc] init];
[cig setTag:arc4random()%666];
[cig setImage:imageView];
[spawnedCigs addObject:cig];
[self.view addSubview:imageView];
}
[self performSelector:#selector(spawnCigs) withObject:nil afterDelay:5];
}
-(void)reset {
scoreLbl.text = #"Score:";
timeLbl.text = #"Time:";
isGameOver = NO;
timeInt = 60;
scoreInt = 0;
cigsOnScreen = 0;
spawnedCigs = [NSMutableArray arrayWithObjects:nil, nil];
[self performSelector:#selector(count) withObject:nil afterDelay:1];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self reset];
[self spawnCigs];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Code is pretty messy so please don't judge me on that.
Thanks for any help that is provided
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[[event allTouches]anyObject];
CGPoint touchPoint = [touch locationInView:touch.view];;
for (UIView *view in [self.view subviews])
{
if([view isMemberOfClass:[UIImageView class]])
{
if (CGRectContainsPoint(view.frame,touchPoint))
{
[view removeFromSuperview];
}
}
}
}
this helps you
use
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UIImageView *view in [self.view subviews])
{
if (view.tag==1)
{
[view removeFromSuperview];
}
if (view.tag==2)
{
[view removeFromSuperview];
}
}
}
try this your issue will be resolved. Don't forget to pass tab to your imageviews...
By using
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
method you will get the image view of selected image. simply remove the object from superview.
Instead of writing all those lines at the init method of HelloWorldLayer :
CCTintTo* tint1 = [CCTintTo actionWithDuration:2 red:255 green:0 blue:0];
CCTintTo* tint2 = [CCTintTo actionWithDuration:2 red:0 green:0 blue:255];
....
CCSequence* sequence = [CCSequence actions:tint1, tint2, nil];
[label runAction:sequence];
I tried to make the label change color forever but got stucked:
I don't know where to place the relavant commands+ dealing with the integers x,y,z
I tried to do the randomize process at the update method,but didn't have any access to the label, any ideas?
// HelloWorldLayer.h
// Essentials
//
// Created by Steffen Itterheim on 14.07.10.
// Copyright Steffen Itterheim 2010. All rights reserved.
//
#import "cocos2d.h"
#interface HelloWorld : CCLayer
{
CCTintTo* tint1;
CCSequence* sequence1;
// CCLabelTTF* label; even tried property
}
// returns a Scene that contains the HelloWorld as the only child
+(id) scene;
#end
//
// HelloWorldLayer.m
// Essentials
//
// Created by Steffen Itterheim on 14.07.10.
// Copyright Steffen Itterheim 2010. All rights reserved.
//
#import "HelloWorldScene.h"
#import "MenuScene.h"
integer_t x;
integer_t y;
integer_t z;
#implementation HelloWorld
+(id) scene
{
CCScene* scene = [CCScene node];
CCLayer* layer = [HelloWorld node];
[scene addChild:layer];
return scene;
}
-(id) init
{
if ((self = [super init]))
{
CCLOG(#"init %#", self);
// enable touch input
self.isTouchEnabled = YES;
CGSize size = [[CCDirector sharedDirector] winSize];
// add the "touch to continue" label
CCLabelTTF* label = [CCLabelTTF labelWithString:#"Touch Screen For Awesome" fontName:#"AmericanTypewriter-Bold" fontSize:30];
label.position = CGPointMake(size.width / 2, size.height / 8);
[self addChild:label];
[self schedule:#selector(update:) interval:1/60.0f];
/*
tint1 = [CCTintTo actionWithDuration:2 red:x green:y blue:z];
sequence1 = [CCSequence actions:tint1, nil ];
id goaction=[CCRepeatForever actionWithAction:sequence1];
[label runAction:goaction];
*/
}
return self;
}
-(void) registerWithTouchDispatcher
{
// call the base implementation (default touch handler)
[super registerWithTouchDispatcher];
//[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES];
}
-(void) update:(ccTime)delta
{
x=(integer_t )(CCRANDOM_0_1()*255); y=(integer_t )(CCRANDOM_0_1()*255); z=(integer_t )(CCRANDOM_0_1()*255);
tint1 = [CCTintTo actionWithDuration:2 red:x green:y blue:z ];
sequence1 = [CCSequence actions:tint1, nil ];
[HelloWorld.label runAction:goaction]; //property label not found on object of type 'HelloWorld'
}
// Touch Input Events
-(CGPoint) locationFromTouches:(NSSet *)touches
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView: [touch view]];
return [[CCDirector sharedDirector] convertToGL:touchLocation];
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint location = [self locationFromTouches:touches];
CCLOG(#"touch moved to: %.0f, %.0f", location.x, location.y);
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// the scene we want to see next
CCScene* scene = [MenuScene scene];
CCTransitionSlideInR* transitionScene = [CCTransitionSlideInR transitionWithDuration:3 scene:scene];
[[CCDirector sharedDirector] replaceScene:transitionScene];
}
-(void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void) dealloc
{
CCLOG(#"dealloc: %#", self);
// always call [super dealloc] at the end of every dealloc method
[super dealloc];
}
#end
If you want the tint color to be random for each time, then you cannot use CCTintTo directly inside CCRepeatForever. You need to re-randomize the RGB values for each CCTintTo action. Thus you need to embed the randomization process inside the action by using block. Here is how:
// do this in init method
__block void (^changeTint)(CCNode*) = [[^(CCNode *node) {
GLubyte x = (integer_t)(CCRANDOM_0_1()*255), y = (integer_t)(CCRANDOM_0_1()*255), z = (integer_t)(CCRANDOM_0_1()*255);
[node runAction:[CCSequence actionOne:[CCTintTo actionWithDuration:2 red:x green:y blue:z]
two:[CCCallBlockN actionWithBlock:changeTint]]];
} copy] autorelease];
changeTint(label);
You should look at CCRepeatForever action. As the name implies, it will repeat the action it points to forever. So you should remove your update method where you are changing the colors, and return to the CCSequence code that you had, and embed that in a CCRepeatForever action:
CCTintTo* tint1 = [CCTintTo actionWithDuration:2 red:255 green:0 blue:0];
CCTintTo* tint2 = [CCTintTo actionWithDuration:2 red:0 green:0 blue:255];
....
CCSequence* sequence = [CCSequence actions:tint1, tint2, nil];
CCAction* repeat = [CCRepeatForever actionWithAction:sequence];
[label runAction:repeat];
I have subclassed UIButton to draw graphics, but the "addTarget" won't work.
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
I did not do anything with making adjustments to that method.
The Button graphics work ok on the "touches".
If I use a standard UIButton then the "addTarget" works fine.
Not sure what I am missing??
thx
#import <UIKit/UIKit.h>
#interface CButton : UIButton
{
CGContextRef c;
int myState;
}
#end
#import "CButton.h"
#implementation CButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
//NSLog(#"init state: %d", self.state);
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// myState = 1;
myState = !myState;
// NSLog(#"BeginState: %d", self.state);
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// myState = 0;
// NSLog(#"EndState: %d", self.state);
// [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
#end
You have to call super from your event handlers when you're all done handling events. How else will the button know of the touches to call your action?
e.g.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// myState = 1;
myState = !myState;
// NSLog(#"BeginState: %d", self.state);
[self setNeedsDisplay];
[super touchesBegan:touches withEvent:event]; // you need this
}
That being said, the comments on your original post are right, subclassing UIButton can get tricky. But it looks as if you have things under control (mostly). Good luck!