I have the following method in my view-controller:
- (IBAction)itemSlider:(UISlider *)itemSlider withEvent:(UIEvent*)e;
{
UITouch * touch = [e.allTouches anyObject];
if( touch.phase != UITouchPhaseMoved && touch.phase != UITouchPhaseBegan)
{
}
else if( touch.phase != UITouchPhaseMoved && touch.phase != UITouchPhaseBegan)
{
}
...
}
Do I really need to link all the events one by one to my controller?
if you are seeking to capture the value changed on slider , then you may follow up the below tutorials instead of using touch events.
http://www.techrepublic.com/blog/software-engineer/better-code-uislider-basics-for-apple-ios/
Related
I'm trying to detect when a pan gesture ends. Under Objective-C, I would do something like the following:
- (void)panGesture:(UIPanGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateEnded) {
...
}
}
In my Swift project, it doesn't recognize UIGestureRecognizerStateEnded... Maybe I just need to import something different(?)
The equivalent is just
gesture.state == .Ended
or if you like
gesture.state == UIGestureRecognizerState.Ended
I have two collisions in my game that happen when the main characters hits an obstacle, game over appears and one where it touches the ground and nothing happens. This are the codes
-(void)didBeginContact:(SKPhysicsContact *)contact
{
if ([contact.bodyA.node.name isEqualToString:#"ground"] || [contact.bodyB.node.name isEqualToString:#"ground"]) {
[hero land];
} else {
[self gameover];
}
How can I add a different main character/hero collision where it doesn't lead me to game over but a whole different outcome (Like a different reaction for the main character)
Try detecting contact for the other special cases:
-(void)didBeginContact:(SKPhysicsContact *)contact
{
BOOL isContactWithGround = ([contact.bodyA.node.name isEqualToString:#"ground"] || [contact.bodyB.node.name isEqualToString:#"ground"]);
BOOL isContactWithHero = ([contact.bodyA.node.name isEqualToString:#"hero"] || [contact.bodyB.node.name isEqualToString:#"hero"]);
BOOL isContactWithOtherThing = ([contact.bodyA.node.name isEqualToString:#"otherThing"] || [contact.bodyB.node.name isEqualToString:#"otherThing"]);
if (isContactWithHero && isContactWithGround) {
[hero land];
} else if (isContactWithHero && isContactWithOtherThing) {
[self doSomethingElse];
else {
[self gameover];
}
}
I have a view with two text fields and two segmented controls. I want each text field to become editable when its corresponding segmented control is selected.
Here is the approach I'm using:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == _textField) {
if (_segmentedControl.selectedSegmentIndex == 0 ||
_segmentedControl.selectedSegmentIndex == 1 ||
_segmentedControl.selectedSegmentIndex == 2 ||
_segmentedControl.selectedSegmentIndex == 3) {
return YES;
} else {
return NO;
}
if (textField == _textFieldTwo) {
if (_segmentedControlTwo.selectedSegmentIndex == 0 ||
_segmentedControlTwo.selectedSegmentIndex == 1) {
return YES;
} else {
return NO;
}
}
}
}
This works for the first segmented control and textfield, but the second text field remains editable regardless of the condition of the second segmented control.
Can anyone tell me where I'm going wrong?
Thanks
The IFs are not nested correctly.
if (textField == _One){
if( 0<= selectedIndex < 4) {
//do something
} else {
return no;
}
if (textField == _Two){
// other stuff
}
}
Can you see that the if(textFieldTwo) is inside the if(textFieldOne) ?
Looks like a simple logic error to me. Your:
if (textField == _textFieldTwo) {
should really be an else if on the first if.
Simply change it to:
} else if (textField == _textFieldTwo) {
and it should start working.
I instance a sprite, and then when it collides with a second sprite, the child of that sprite is removed:
if (CGRectIntersectsRect(spriteOne.boundingBox, self.swat.boundingBox))
{
if (spriteOne.tag == 0){
[self removeChild:spriteOne cleanup:YES];
}
if (spriteOne.tag == 1){
[self removeChild:spriteOne cleanup:YES];
}
}
This works how I want it to, and the sprite disappears off the screen. However, it seems that the boundingBox is still there, even if the image is not, and this causes trouble with scoring, etc... So, what I would like to know is how to 'dis-activate' the sprite's boundingBox so that when the first time that the two sprites colide, the collision is detected, but any time after that, it is not.
Thanks in advance.
As far as I understand, you should have some issue with removing the sprite after a collision.
Would you try this?
if (CGRectIntersectsRect(spriteOne.boundingBox, self.swat.boundingBox))
{
if (spriteOne.tag == 0){
[spriteOne removeFromParentAndleanup:YES];
}
if (spriteOne.tag == 1){
[spriteOne removeFromParentAndleanup:YES];
}
}
Have you tried adding some NSLog traces to see whether the sprite is really removed?
You must be retaining spriteOne. If there is a good reason to keep it around, do this:
if ( spriteOne.visible && CGRectIntersectsRect(spriteOne.boundingBox, self.swat.boundingBox))
{
if (spriteOne.tag == 0){
spriteOne.visible=NO;
}
if (spriteOne.visible && spriteOne.tag == 1){
spriteOne.visible=NO;
}
}
Later, when you need spriteOne in play again, just set its visibility to YES;
If not, you have a leak, and do this:
if ( spriteOne && CGRectIntersectsRect(spriteOne.boundingBox, self.swat.boundingBox))
{
if (spriteOne.tag == 0){
[self removeChild:spriteOne cleanup:YES];
self.spriteOne=nil; // assumes you have a property for spriteOne
}
if (spriteOne && spriteOne.tag == 1){
[self removeChild:spriteOne cleanup:YES];
[spriteOne release]; // assumes no property for spriteOne
spriteOne=nil; // dont forget this ! beware of zombies
}
}
In my game, I have about 6 different variations of objects. Each object has a b2Body, b2BodyDef, and b2FixtureDef attached to it.
In my case, my b2Bodys are following my CCSprites since Cocos2D is easier with animations.
Anyway I am trying to follow Ray Wenderlich's tutorial: http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone
The thing is I am quite confused on what he is actually doing!
Questions:
1. Does my Contact Listener code in my CCScene need to be in my game loop?
2. This is his main code for his collision detection in his CCScene:
3. Also I see that in the below code he uses tags for his CCSprites, does that mean my CCSprites do not have to be ivars? Also what about my b2Bodys, b2BodyDefs, and b2FixtureDefs, do they have to be ivars? Does he just do it by tags?
std::vector<b2Body *>toDestroy;
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();
if (spriteA.tag == 1 && spriteB.tag == 2) {
toDestroy.push_back(bodyA);
} else if (spriteA.tag == 2 && spriteB.tag == 1) {
toDestroy.push_back(bodyB);
}
}
}
std::vector<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
if (body->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *) body->GetUserData();
[_spriteSheet removeChild:sprite cleanup:YES];
}
_world->DestroyBody(body);
}
if (toDestroy.size() > 0) {
[[SimpleAudioEngine sharedEngine] playEffect:#"hahaha.caf"];
}
The thing is, like before he only has 2 things that CAN collide. In my case I have 6. So would I have to modify that code in any way to work with my 6 possible b2Bodys?
Thanks!