Touch a sprite without boundingBox - cocoa-touch

So i'm trying to move multiple sprites on the screen at the same time. The problem is that if the 2 sprites are close to each other the position becomes the same (so the bounding box is the same) and i can't seem to pull them apart.
I'm using the "standard" CGRectContainsPoint(sprite1.boundingBox,location).
What i need is to get the sprite on top regardless of boundingBox. Any ideas?

One way would be to start assigning explicit z values to sprites you add, using CCNode's -(void) addChild: (CCNode*)node z:(NSInteger)z method.
Then, when you get multiple sprites back from your bounding test, only move the one with largest z value.

hah..i fixed in the easiest way possible :\
if (CGRectContainsPoint (sprite1.boundingBox,location)){
sprite1.position=location;
}else if (CGRectContainsPoint (sprite2.boundingBox,location)){
sprite2.position=location;
}
the way this behaves is that if the bounding boxes are the same..it only takes one..not the 2nd one

Related

Sprite in Game Maker doesn't act the way I want it to

I'm currently working on animating my player so that he behaves like he's breathing.
if(time mod 60==0){
if(image_index==0){
image_index=1;
}
else{
image_index=0;
}
}
time++;
The whole thing is put in the step event and sprite is changing every single step and it even changes to an index 2 and 3, which I haven't even used in the code.
So if anyone has some ideas why does it work like this then please tell me.
It is because the sprite you use has multiple sub-images. GameMaker will naturally iterate the image index every frame.
So first, you need to stop the animation from running with
image_speed = 0;
You have to run this line when the sprite has just been changed, so ideally just after the "sprite_index" variable is changed. If you don't change it, just set image_speed to zero in the creation code.
If you are curious, I found the answer here : How to freeze sprite animation on last frame?

How to make an SKNode continue in the same direction after finishing following a path

In my project I have an SKNode following a CGPathRef made with touchesMoved. What I want to have happen is once it reaches the end of the CGPathRef, it follows in the same direction that it ended in. I am using the SKAction followPath and tried to implement this by adding a line from the last point on the path (touchesEnded) all the way to the end of the scene using some previous points and a unit vector however this was not only complicated but also very inconsistent. Is there another method I can use to make it just continue on its path after it finishes the CGPath? Thanks!
I suggest storing your node's CGVector (dx, dy) which represents the direction of movement for your node. For example, if the last moving vector was 15,15 that translates to your node having moved up and to the right. You can use this value to continue moving. If the values are too low or too high, you can add a min/max filter.

How to detect if a button is in a desired area in Objective c

How can I detect if a button is in a desired area
I have used if( button.frame.orgin.x == area.framee.orgin.x){
if (button.frame.orgin.y == area.frame.orgin.y )};
The Problem with this code is that it is very exact so it is hard to match up the button with the label. So I would like to know how to detect if a button is inside a desired area. And how to make the area bigger than the button. Thanks In advance
Your best option is probably CGRectContainsRect() which would work something like:
if(CGRectContainsRect(someRect, button.frame))
{
//Button is in area.
}
To expand the rect you can use CGRectInset(), passing negative values to increase the frame size by that amount. This function will maintain the center of the original rect.
CGRect newRect = CGRectInset (smallRect, -10.0f, -10.0f);
You can read more about CGGeometry functions in the docs.
Use GCRectContainsRect(frame1, frame2); to see if an object is contained within another object. Or use CGRectIntersectsRect(frame1, frame2); to see if the rects overlap at all.

looping a sprite vertically objective C sprite builder

Note: for this I am using a program called spritebuilder, which allows me to create a game with less code than would normally be needed. If you know a solution that's just all code, then by all means feel free to share it :)
Also, for this question, I followed a tutorial at this link: Build Your Own Flappy Bird Clone. Just scroll down to the part that says: "Loop the Ground"
So here's my problem. Im currently working on a game, and I created a camera which scrolls vertically long with the character sprite i created, however i need a certain image to loop. When the image leaves the bottom part of the screen I would like it to loop around to the top of the screen, infinitely. For this i created two identical images (in this case its the bark of a tree). One will be on screen, while the other will be offscreen, so as the first image leaves the screen, the second will replace it (seamlessly). I created two objects for the images, and assigned them the name _ground1, and _ground2, and I also created an NSArray in which to store them in. (Please refer to the link above if it is somewhat confusing)
Here is the code that I have:
CCNode *_ground1;
CCNode *_ground2;
NSArray *_grounds;
for (CCNode *ground in _grounds) {
// get the world position of the ground
CGPoint groundWorldPosition = [_physicsNode convertToWorldSpace:ground.position];
// get the screen position of the ground
CGPoint groundScreenPosition = [self convertToNodeSpace:groundWorldPosition];
// if the left corner is one complete width off the screen, move it to the right
if (groundScreenPosition.y <(-1 * ground.contentSize.height)) {
ground.position = ccp(ground.position.x , ground.position.y + 2 * ground.contentSize.height);
}
For some reason when I try this, it doesnt seem to work. what happens is that, the camera will travel vertically as it is meant to do, but the images do not loop. Once the two images leave the bottom of the screen, no new images replace them.
i also done this project as above tutorials. it work fine but you have some mistake to set variable in spritebuilder. in your above code replce code as and try it. you only put less than may be it issue.
if (groundScreenPosition.y <=(-1 * ground.contentSize.height)) {
ground.position = ccp(ground.position.x , ground.position.y + 2 * ground.contentSize.height);
}
You are using CCNode objects as _ground1and _ground2.
CCNode objects usually do not have a contentSize, they will return 0 unless you explicitly set them inSpriteBuilder`.
Make sure that you are using CCSprite objects in SpriteBuilder and in your code.
Also, as a friendly hint you should also consider refactoring (renaming) your sprites with more meaningful names for your use case like _treeBark1 and treeBark2 for example.

How do you measure how far the map moved?

In my mapView:regionDidChangeAnimated method I'm making a call to find places on the map but I only want to make the call if the map has moved a significant amount.
Here is scenario:
User moves map or map load
HTTP call to get find places
Add places to the map.
PROBLEM! User clicks on an annotation opening the title bubble and it's close to the edge so it moves the map. Since the loading of data is tied to the map move event the marker disappears and is re-added.
How should I watch both the span and the center point for change?
#Scott Thanks for the visibleMapRect idea. This is what I have working so far, it still needs to account for zooming in and out.
MKMapRect newRect = _mapView.visibleMapRect;
MKMapRect oldRect = currentRect;
float leftBoundry = (newRect.origin.x-(newRect.size.width/4));
float rightBoundry = (newRect.origin.x+(newRect.size.width/4));
float topBoundry = (newRect.origin.y-(newRect.size.height/4));
float bottomBoundry = (newRect.origin.y+(newRect.size.height/4));
NSLog(#"Origin x %f, y %f", oldRect.origin.x, oldRect.origin.y);
NSLog(#"Boundries left %f, top %f, right %f, bottom %f", leftBoundry, topBoundry, rightBoundry, bottomBoundry);
if (oldRect.origin.x < leftBoundry || oldRect.origin.x > rightBoundry || oldRect.origin.y < topBoundry || oldRect.origin.y > bottomBoundry) {
[self loadLocations];
currentRect = newRect;
}
Hmm. It sounds like you're refreshing your map by removing all annotations, and then (re-)displaying all annotations that fall within the visibleMapRect – and that solving this problem may require a more nuanced approach to updating the map.
One approach might be to use MKMapRectIntersection to identify the overlap between your "old" and "new" visibleMapRects, and (if there is one) exclude the annotations in this region from being removed or re-added. Or, you could calculate the L-shaped area that's scrolling on-screen and only make an HTML call for data within that region.
Or you could just check whether the map's "old" center is still within visibleMapRect, and if so arbitrarily decide that the map hasn't moved a significant amount. That may leave you with areas on-screen that should have annotations but don't, though.
Or, finally, you could just store the coordinate of the user-selected annotation, and if that coordinate is still on-screen after the map moves, find it and re-select the annotation.