I'm a beginner when it comes to iPhone development and i've searched for the answer to this question and haven't found anything yet.
What i'm trying to do is allow the user to move an object without actually touching the object (they can touch anywhere on the screen) and as they move the object will go from where it was originally and move in the direction of the users movement but not jump to thier touch location and be covered up by their finger.
Any help you can provide would be phenomenal
Thank you in Advance,
BWC
You can subclass a UIView to track the touch movements by overriding the touchBegan:withEvent:, touchMoved:withEvent: and touchEnded:withEvent messages. In these messages, track the UITouch objects' locations (converted to points on the view by using the convertPoint:toView: message of the UIView you're subclassing) and calculate the difference of movement; then apply the same difference to your object, wherever it might be on the screen.
I know this is old.... But here is a great tut doing this with Cocos2d and sprite animations.
http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d
Derek
Related
I need help implementing controls to move a SceneKit sphere. I want to have the user touch on the screen and when they move their finger around, the ball will move relative to the position of the user's finger. I don't want the y to increase or decrease I just want x and z, almost like a hula hoop.
I think you can use the GameController Framework seen Apple have released the WWDC Demo 'Fox', and it use GameController to control the character.
I am aware of how to use the SpriteKit physics engine to detect when a collision occurs between two nodes, however, I'd like to be able to detect this and also get the direction relative to one of the objects that the collision occurred. Incase anyone is curious, I'm asking this so I can make a platform game and I need to detect when something is resting on the top of the floor. Any help is appreciated, thanks!
Implement the SKPhysicsContactDelegate on one of your objects (perhaps your scene object). Set your scene's physicsWorld.contactDelegate to the object.
In the contact delegate's didBeginContact(_:) method, you are given an SKPhysicsContact object. The contact's contactNormal property should be the direction you're interested in.
I am making a game in Cocos2d. It has a 'shoot' button. How can I make it so that it:
1. Recognizes that the button sprite was touched
2. Perform a method that will actually load the bullet graphic onto the layer and
3. Have it moved at a steady pace across the screen (like a real bullet)
Any help is appreciated. Thanks!
Great questions, all three, and all are covered in the various Cocos2d getting started books (Yes, books!) and guides! To answer your questions specifically would require that you answer a number of questions about how you've set up Cocos2d in the first place; what kind of map you are using, whether and which physics engine, etc....
If you haven't answered those questions yet, best to start by researching Cocos2d.
Start here.
I haven't read the book(s) available, but Ray Wenderlich's Cocos2d tutorials were incredible helpful and demonstrative.
If you're subclassing CCLayer, set the isTouchEnabled property to YES. You then receive callbacks like -ccTouchesBegan:withEvent:, -ccTouchesMoved:withEvent:, etc. Your button handling code would go in those methods. See the documentation on CCLayer and related classes for more information.
Set the isTouchesEnabled to TRUE in init. And just get rectangular co-ordinates around your button sprite and then in touchesBegan just check whether the touched location is inside the rectangular button area, if it is inside it just call your respective function.
Which is the best approach to create a side Scrolling racing game?
Should I just move the background or work with the camera instead? I read in lots of places that moving the background is the way to go, but how do I handle the other players?
Sorry if this is a noob question but I'm a bit confused.
i think the best way is to set a point in your scene called ancher which doesn't move in your screen but and calculate every other position relative to it. the ancher is moving along side every other player but in a controled manner (for example you can set it in the middle of your track along side with player), in this case you only have to move the background according to your ancher and then you calculate the other positions all relative to the ancher. it may look a bit complicated but implementing this is much more easier and bugfrier than any other method.
Bind all your players to your background (make them children). Then no handling will be required at all
If you want lots of customization then make your own methods for moving the background, players, etc.
If you just want simple player following then use:
[self runAction:[CCFollow actionWithTarget:(the player's CCLayer) worldBoundary:the World's Boundaries in CGRect]];
or simpler:
[self runAction:[CCFollow actionWithTarget:(the players CCLayer)]];
Is it possible to get the array of touches positions from the screen without getting it from touch events?? If there were no touches in the screen the array would be empty. Any idea??
If there's a cocos2d method for this, would solve too...
Thanks in advance.
There is no way to get 'out of band' touches in CocoaTouch. I am unsure if Cocos keeps the information around that would be required to do this, but if it doesn't it wouldn't be that hard to track current touches and what state they're in.