I'm creating a game, and currently if you touch the left half of the screen a gun rotates clockwise and if you touch the right half of the screen it shoots.
I want to create an action so that if I touch the left half of the screen it will rotate clockwise and if you touch the right half it rotates counterclockwise. THIS IS EASY.
I want the shooting to happen if I'm touching the left and right half's at the same time. How is this possible to detect 2 touches at the same time.
This is the code for rotating clockwise if touching the left half and shooting when touching the right half
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
if (touchPoint.x < self.size.width / 2.0 ) {
isTouching = true;
}
else
[self setupBullet];
}
You will need to either setup two separate objects that listen for touch events in their respective side, or you can use the event.allTouches object and get a complete array of all current touch objects and do with that array as you will.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//either:
if(touch in left side)
{
//Left side touch down, create object
}
else
{
//Right side touch down, create object
}
//Or:
NSArray *arrayOfAllTouches = event.allTouches;
//Now loop through that array and see what is going on on screen
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//either:
if(touch in left side)
{
//Left side release, release object
}
else
{
//Right side release, release object
}
//Or:
NSArray *arrayOfAllTouches = event.allTouches;
//Now loop through that array and see what is going on on screen
}
Related
I have to do some "snap to grid" behaviour,
I have a dragging image "sprite", but I need to "snap" this sprite to a certain button if touches go on top of that button,
so how can i know if touchesEnded is on top of a certain button,
here my code, but i dont know when touches where ended on top of butotn
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touched = [[event allTouches] anyObject];
CGPoint location = [touched locationInView:touched.view];
CGRect recta = touched.view.frame;
if (CGRectIntersectsRect(recta, self.button_1.frame)) {
DLog(#"intersected");
}
}
So this is not happening,
can it be done?
or do i have to check the X and Y position of finishing touch against the button frame X and Y position by my self?
cheers
touched.view is the view on which the touch started. You want to find out if the location where the touch ended is on top of a specific button. You can do this by checking if the location of the touch relative to the button is within the button's bounds.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touched = [[event allTouches] anyObject];
CGPoint locationRelative = [touched locationInView:self.button_1];
if (CGRectContainsPoint(self.button_1.bounds, locationRelative)) {
DLog(#"on top of button_1");
}
}
You can use CGRectIntersectsRect, which returns YES, if the two rectangles your pass to it intersect.
I would like my sprite to move from one side of the screen to the other like this:
Before tapped:
http://imgur.com/wIFa3JL
And then after the screen has been tapped:
http://imgur.com/ZtWiE7b
Would it start with something like this:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
Thank you!
I am not quite sure if you want to get the position of your finger when you touch the screen and apply to the sprite's position or just to change the position of the sprite when you touch the screen.
If you want to get your finger's position inside touchesBegan and apply that position to the sprite:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
CGPoint location = [touch locationInView:self];
spriteNode.position = location;
}
}
If you want to just change the sprite's position to the opposite side of the tree every time you touch:
First you need to keep track of which side your sprite is currently. Let's create a BOOL to hold that info:
BOOL isRightSide;
Then, at the beginning, if your sprite start at the right side of the tree, just assign TRUE to the boolean.
Finally, in the touch event:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (isRightSide) {
//Change to sprite's position to the LEFT side
} else {
//Change to sprite's position to the RIGHT side
}
isRightSide = !isRightSide;
}
I hope this helps =)
In my current objective C project I am coding a mechanic that when you drag your hand on one half of the screen an object moves in direct correlation and when you drag on the other half of the screen, the other object moves in direct correlation but the first does not. When I test my project the first object moves perfectly on the half screen, however the second object does not when the other half of the screen is touched
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (location.x <= 259 )
[Person setCenter:CGPointMake(location.x, Person.center.y)];
if (location.y >289)
[Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (location.x <= 259 )
[Person setCenter:CGPointMake(location.x, Person.center.y)];
if (location.y >289)
[Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
}
Your objects should be moving just fine. However, if you're trying to split your screen in half (as you said in your question) then your if statements in your touch delegate methods are rather odd. Here is an image of which "Person" object will be moved depending on where you touch the screen (assuming 4" screen in portrait)
As you can see, there is a section of screen that will not move either object, and another (rather large) section that will move both of your objects. There is only a very small area that will move Person1 by itself.
If you're wanting to assign half of your screen to each object, I would suggest doing something like this to split the top and bottom half of the screen:
if(location.y <= self.view.frame.size.height / 2)
{
// move Person
}
else
{
// move Person1
}
You could obviously modify that to split the left/right halves of the screen.
If you're still having issues moving both objects, make sure that they're hooked up to the view if they're IBOutlets
I want to shift the position of some UILabels when the user swipes the screen--specifically, move them the same distance the user swiped their finger.
How would I go about detecting and implementing this?
Thanks
Override touchesBegan:withEvent and touchesMoved:withEvent:. Keep track of the starting location in touchesBegan:withEvent. If repeated calls to touchesDragged (it is called while the drag is happening) show that you are moving fast enough for the action to be a swipe versus just a drag, use the difference between the starting location and the current touch location to animate changing the UILabels.
You can do that by using following methods for UIView.
In touchesBegan you should store the position where the user first touches the screen. So you can identify the left or right swipe when touches ends.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
self.startPoint = [touch locationInView:self];
}
Record the final position on touchesEnded method. By comparing this two positions you can determine the direction of movement.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint endPosition = [touch locationInView:self];
if (startPoint.x < endPoint.x) {
// Right swipe
} else {
// Left swipe
}
}
I hope this helps.
I'm building a game using Sprite Kit that needs quick precise movements following the user's touch. I need to detect if the user has touched on the "Player" in the view, and if they have, when they move, the player sprite needs to move with the touch accordingly.
I have this working now, however, it's not very precise... the more movement input (without lifting your finger), the more offset the sprite gets from the touch location.
Here's the code I'm using right now.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if (self.isFingerOnPlayer) {
UITouch* touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
CGPoint previousLocation = [touch previousLocationInNode:self];
// Calculate new position for player
int playerX = player.position.x + (touchLocation.x - previousLocation.x);
int playerY = player.position.y + (touchLocation.y - previousLocation.y);
// Limit x and y so that the player will not leave the screen any
playerX = MAX(playerX, player.size.width/2);
playerX = MIN(playerX, self.size.width - player.size.width/2);
playerY = MAX(playerY, (player.size.width/2)-3+inBoundsOffset);
playerY = MIN(playerY, (self.size.height - ((player.size.width/2)-3) - inBoundsOffset));
// Update position of player
player.position = CGPointMake(playerX, playerY);
}
}
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch* touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
SKPhysicsBody* body = [self.physicsWorld bodyAtPoint:touchLocation];
if (body && [body.node.name isEqualToString: #"player"]) {
NSLog(#"Began touch on player");
self.isFingerOnPlayer = YES;
}
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
self.isFingerOnPlayer = NO;
}
It detects the touch location, checks to make sure that you're touching the player sprite, if you are, then when you move, so does the sprite... but it can get off quite quickly if you're slinging your finger around (as playing this game will cause the player to do).
Can anybody suggest a more accurate way of accomplishing this, that will keep the sprite under the user's finger even when moving around a lot without lifting your finger?
You have to keep in mind that -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event ONLY gets called when the moving finger writes (sorry couldn't help the Inspector Clouseau reference).
In effect what happens is that the user can very quickly move his/her finger from one location to the next and as soon the the finger is lifted, your position updates stops.
What I suggest you do is create a CGPoint property and have -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event store the location in the CGPoint property.
Then in your -(void)update:(CFTimeInterval)currentTime you add the code that actually moves the player to the finger coordinates. This should make things a lot smoother.