cocos2d collision not detected in if((self=[super init])) - ios

I used a CGRectIntersectsRect to detect the collision of a moving image (anvil) and an image (guy).
The "anvil" moves constantly in a CCRepeatForever and the "guy" moves in a ccTouchesBegan method by the control of the user.
Here it is, very simple:
if (CGRectIntersectsRect(guy.boundingBox, anvil1.boundingBox))
{
pancake = [CCSprite spriteWithFile:#"pancake.png"];
pancake.position = ccp(200,200);
[self addChild:pancake];
}
The problem is that the above lines of code do not work in the if((self=[super init])) and only in the ccTouchesBegan, where I have all of the actions for "guy." I do not want to have the image "pancake" appear only if the user taps exactly when the two images collide...if the images EVER collide, regardless of touch events, "pancake" should appear.
Any advice? Thanks in advance!

Not to criticize but your code doesn't make a lot of sense. Why would you be testing for interestion in an init method for two objects that are constantly in motion throughout your scene? Even if you wanted to show the pancake whenever the two intersected, why don't you load the pancake and make it invisible, then switch it to visible whenever the two objects intersect. This could be done simply by putting the condition in an update method and setting pancake's visible property to true if they intersect or false otherwise.
But to answer the question of "why" that code doesn't work in init is simply because at the point of calling this either one or both of your objects doesn't exist, they simply don't intersect, or their bounding box properties have not been set. I bet they simply don't intersect yet. But that doesn't matter much since there is no reason I can see to check for interestion in an init method. Schedule the update method, create the update method, and place this condition inside of it but rather than create the pancake sprite, you'd be setting the already existing pancake sprite's visibility to TRUE.

Related

SpriteKit prevent more than one touch at a time

I am making a SpriteKit game I was wondering if there was a way to prevent more than one touch at a time.
in my game an object gets added at every touch and I kinda don't want that. (even though its hilarious) if there is a way how do I do it? what would I use? and could you point me in the right direction? and I know that there are ways to do it as I have seen multiple games with that feature.
would I put something into 'appdelegate.swift' to prevent that or would it have something to do with the 'touches began' function I have tried several methods but none seem to work also I have searched all over google but to no avail.
if somebody could help me with this I would appreciate it but its not really that important as it doesn't upset the balance of the game at all.
You can use multipleTouchEnabled property of a UIView:
When set to YES, the view receives all touches associated with a
multi-touch sequence and starting within the view's bounds. When set
to NO, the view receives only the first touch event in a multi-touch
sequence that start within the view's bounds. The default value of
this property is NO.
Use it like this self.view.multipleTouchEnabled = false, where self is a scene.

SpriteKit Objective-C – Detect how much finger has moved in -touchesMoved

I'd simply like to know if there is a way to detect how many pixels the finger has moved during the -touchesMoved function?
EDIT:
This is what I've tried. I made two instance variables called _previousPosition and _currentPosition. In -touchesBegan, I set them both to be the current finger location in the scene. In -touchesMoved, I set _currentPosition to be the current finger location once again. Keep in mind that during -touchesMoved, when I'm updating _currentPosition, _currentPosition is being constantly updated, while _previousPosition is not. Finally, in touchesEnded, I create another variable (not global, but private) called pixelsMoved, and set that equal to _currentPosition - _previousPosition. Right after that, in -touchesEnded, I reset _previousLocation to be the current finger location. It's all very complicated, so I'm almost positive I've made some mistake somewhere. Any help would be appreciated.
I'd simply like to know if there is a way to detect how many pixels the finger has moved during the -touchesMoved function?
-touchesMoved:withEvent: provides an event, and from the event you can get individual touch objects, each of which have an associated location that you get with -[UITouch locationInView:]. You don't get information about how far the touch has moved since the last time you looked, but you can keep track of the location of each touch and do the comparison yourself.

Cocos2d scaling sprite causes artifact

I am on the master branch so that it will work with ARC.
I have implemented a method whereby the sprite will scale by a factor of 1.1 when a user touches the sprite. Multiple touches will queue up multiple scaling actions, built on top of each other. Every now and then I get a strange artifact where the smaller version of the sprite shows up on top of the scaled version.
Here's a screenshot:
More background: I'm using a texture atlas so I use:
sprite = [super spriteWithSpriteFrameName:anObject.filename];
to initialize the sprite. Is this a bug in openGL/cocos2d? Any advice on how to stop this artifact?
EDIT:
I am subclassing CCSprite but as far as I can tell there is only one instance of the sprite (the call to super was in a class method). Basically the user will define a list of actions that the sprite will do. The action list can also be interrupted using:
[self stopAllActions]
I've had actions using subclasses of CCMoveBy and CCRotateBy with no issues. It's only the most recent subclass of CCScaleBy that is causing this artifact. In the subclasses of these actions I'm not changing anything in the actions, just tracking certain variables so that I can properly resume the action after the interrupt.
Are you perhaps subclassing CCSprite, and in your subclass did you add a CCSprite instance variable as well?
In that case you'll be showing two sprites. The super class sprite and the instance variable sprite. If the other sprite shows up only sometimes this may depend on the order of adding sprites as childs, or the zOrder property.

Update loop and buttons in cocos2d

I have a simple lunar lander game.
I compute positions and everything by integration - e.g. each turn I take vectors and combine them and then apply resulting vector to my lander.
Here comes the question, I have a button that I want to use for thrust.
How do I check if it is on during update method? I guess i will have some BOOL flag that gets set to YES when the button is pressed, but when do i set it to NO?
Some practical implementation would be great.
I use cocos2d-iphone and iOS.
Well, the pseudo code goes as follows:
We shall not use Buttons (aka CCMenuItem), since they provide callbacks only on touch up events. We want touch down, touch exit/entered, touch ended.
In your CCScene that you are displaying, either add a new child that is a subclass of CCLayer or even use one of the CCLayers already present in the CCScene.
In the init of your CClayer subclass, set isTouchEnabled to YES.
Implement the usual methods:
- (void)ccTouchesBegan:...
- (void)ccTouchesMoved:...
- (void)ccTouchesEnded:...
- (void)ccTouchesCancelled:...
Finally, do your magic in these methods.
Get the touch location
Check using CGRectContainsPoint whether the touch is within the thrust area.
and so on, and so forth...

Creating a scrollview

Here's my current situation:
I created a CCScene named StoreScene.
Within that scene, I've initialized a CCLayer and just named it Store layer.
Now, I want a scrollable layer that contains content which the user can touch.
To do this, I created another CCLayer named Store Container.
I create an instance of this layer and add it as a child to StoreScene.
In StoreContainer, I've added multiple sprites, each with a unique tag.
The scrolling is done within the StoreScene and touches will move the entire Storecontainer layer up or down.
I have added 4 sprites to my scrolling layer (store container)
Initially,
sprite 1 is located at 0,10
sprite 2 is located at 0,20
sprite 3 is located at 0,30
sprite 4 is located at 0,40
Obviously, as the entire storecontainer layer shifts, the sprite positions shift as well.
However,
when I do this in the TouchesEnded method:
if (CGRectContainsPoint(sprite1.boundingBox, touchpoint)){
NSLog(#"TouchedSprite1");
}
... and so on for each sprite
The touch locations of each sprite remain in the same place!!!
Visually, the sprites are moving up and down nicely.
but their locations when receiving touches stay constant..
is there a reason for this?
Any other way for approaching a scrolling layer?
I've already looked at the UIKit's scrollview and I've looked at CCScrollLayer and both aren't good enough for me.
My way may be simpler but it doesn't work as planned.
Thanks in advance! ^_^
I'm guessing you've implemented touchesEnded in your StoreContainer. In that case the touches' coordinates will be relative to that layer, which explains why their coordinate system is following the layer around the screen. You could implement touchesEnded in the scene instead, or put the StoreContainer instance inside a new and immobile layer whose purpose is only to handle touches.
Alternatively you could keep your current setup and use your answer to this question to get the touch coordinates in the world... :)
Check out CCKit, there is a nice CCScrollLayer implementation included.

Resources