IOS Objective-C moving multiple objects with just the UIKit - ios

The problem I am facing is that I have absolutely no idea how to create multiple balls that move with their own speed, have their own location. That kind of thing. When I click the screen, a ball should start moving up. When I click again, another ball should start moving up. You get the idea. I've looked at several source codes and tutorials but I really don't understand how this is achieved?

You should probably be doing this with SpriteKit (see video)
But if you want to keep it really simple, your approach could be something like:
Create a Ball class (object) (this could, if you want, contain the image of the ball)
Initiate a new Ball object on every touch: Ball *ball = [Ball alloc] init]];
Start animating the newly created ball (using basic animation for instance)

Related

Ideas on how to manage movement of SKNode related to other, moving down but maintaining the same alignment with the other SKNode

i'm new in the development of swift ios game using spirtekit and i need help. I want to develop a vertical 2d endless run, where with a swipe you are able to move the player in direction and if there is an obstacle, it is created in a position calculate with the position of the player. Once create the obstacle i would like to make it falling down but alway with the same relation with the player (this means that it has to move only on y axis).
Can you suggest me some ideas on how to manage this?
This book teaches you exactly how to make what you are looking for:
https://www.amazon.com/Getting-Started-SpriteKit-Jorge-Jordan-ebook/dp/B01891X7TM
This is the end product:
https://itunes.apple.com/lr/app/inside-the-hat/id1076967804?mt=8
You can get it for free here (with trial):
https://www.packtpub.com/application-development/getting-started-spritekit
The source code for this is around somewhere on the web, but I forgot where :( I bought the book a year ago and can't find it now.
Basically, you have a long background that is .moveTo .. in .update you check the position of the background, then reset it based on its XY position (it basically just goes all the way down, warps to the top position, then falls)
Also in .update you check for score, or a timer, or just a random check, then spawn an enemy, obstacle, etc. You give the enemy / obstacle SKActions to move towards the player, and let .didBegin(contact:) handle scoring, sound effects, death, etc.

SpriteKit: Move Object on Rope along a Track / Conveyor Belt

I have a sprite hanging from a rope in SpriteKit. I have the rope pinned to an object I'd like to slide back and fourth (to simulate the rope being attached to a track or conveyor belt). Imagine clothes hanging on the revolving rack of a dry cleaners store. Similar to that!
Anyway, I can't figure out the proper way to move my rope as if it its connected to the track.
Pinning the rope to another (track) object and moving that track object doesn't seem to work. I can't apply move actions to a sprite that has physics (without the simulation getting all messed up).
I've also tried adjusting the track objects velocity directly to (2,0) in the update method (Including all the other update callbacks SpriteKit surfaces). The only problem is the weight of the rope and attached object eventually pull the track object down. I think this happens because didSimulatePhysics happens no matter what.
Any suggestions on how to move my sprite on a rope along a track?
Thanks in advance!

Draw circle and drag touch around SKSpriteNode

I am making a Sprite Kit game where the player (basically a stickman) has a running animation and a parallax scrolling background.
Now I have enemies that come near my player. To destroy these enemies sometimes I have to touch the enemies node to launch a rocket or attack them with an attack button or just jump over them.
Everything is working fine, but I want to add some extra moves to destroy them. I want some enemies that you can just destroy if you have drawn a whole circle around them. So imagine they come and you make a circle and then my player launch a laser or something. The problem is I have no idea where to start.
I haven't found anything on the internet. If it's too complicated or almost impossible how about touching my player node and dragging to the enemy?
EDIT: I think I have to create a custom GestureRecognizer that recognizes if a circle is drawn around a sprite and then runs the code. I don't know how this works ?
Yes, it's too complex. Not just from a coding point of view, but also from that of the player's experience.
Anything that requires complex gestures over a large amount of glass is annoying for the player because they're never going to have the same experience. Their finger's moisture and oil content always changes, as does the ambient temperature and cleanliness of their screen.
So big gestures required to be performed quickly (a gaming input like this) will sometimes be fun and smooth, and other times degrade as an experience based on the nature of the above properties.
Best to avoid them for a game's best possible experience.
If you must do it, there's two ways to research how.
Seek out "custom gesture" creation and utilisation through documentation and google, etc.
Think about using some kind of array to store all the points where the player's finger moves through during that circle gesture and attempt to discern if an enemy is within that space and then act accordingly.
--- probably other ways, too. But these jump to mind.

Sometimes sprite is pushed away when touches another sprite

I am making a game using cocos2d and SpriteBuilder. There is a hero sprite and coins, that should be collected by him. That's a horizontal scrolling game, so every time hero touches a coin, this coin changes it's x-coordinate 2000px to the right and new y-coordinate is generated at random. Using update method I move it to the visible area as a "new" coin. But when hero flyies by and doesn't collect it ,coin must change coordinates only when it's already off the screen, so I tried this solution:
-(void)update:(CCTime)delta{
_coin.position=ccp(_coin.position.x - delta * scrollSpeed, _coinY);
if (CGRectIntersectsRect(_hero.boundingBox,_coin.boundingBox)) {
_coinY=arc4random() % 801 + 100;
_coin.position=ccp(_coin.position.x + 2000.f,_coinY);
}
else if(_hero.position.x >= _coin.position.x + 150){
_coinY=arc4random() % 801 + 100;
_coin.position=ccp(_coin.position.x + 2000.f,_coinY);
}
It works,but after that I found a small bug(I am not sure, whether it's related to this code) : sometimes, when hero touches coin, hero is like pushed away to the left. I have no idea why.
How to fix it?
Is that way, that I am using for coin ,right?
I see 2 issues with your approach:
1. Hero bounces of the coin.
To fix this you need to make your coin a sensor. This way you will still be notified about collisions, but the hero will simply pass through the coin without actually hitting it.
I'm not sure if you can set this in SpriteBuilder, but probably you can enumerate all coins after loading the scene and set sensor property to YES:
coin.physicsBody.sensor = YES;
Things like this is one of the reasons I believe you first need to learn pure Cocos2D and only then use tools making your life easier such as SpriteBuilder. Which is a great tool, but in some cases you still need know what happens behind the scenes.
2. You're mixing physics with things like CGRectIntersectsRect
If you're using physics engine you need to detect collisions via collision delegate and not by checking CGRectIntersectsRect in update:.
It is hard to explain how to do this in a few sentences, so here is a nice tutorial that shows how to detect collisions in Cocos2D v3 and of course there is a chapter about this in my book.
By the way you shouldn't use update:at all when manipulating physics nodes, use fixedUpdate: instead.
I hope this will help you to solve your issue.

Sprite kit ios game slingshot machanism similar to angry bird game

I am newbie in IOS Gaming, and i need to create a game using Sprite Kit framework for functionality similar to angry bird pulley system, and also wants to find the distance the object is travelled from the pulley to its landing.
Can anyone help me out with this, i would be thankfull for it.
Thanks in advance.
One way you could code a slingshot effect would to use a starting point on the screen at let's say (x=100,y=100). You would display a SpriteNode of a slingshot with the Y centered at the (100,100).
The next step would be to use touchesBegan:withEvent: in the area of the slingshot to let your code know the player is looking to shoot the slingshot.
You would use touchesMoved:withEvent: to track how far back (how much tension) the player is pulling back from the slingshot.
The release would be triggtouchesEnded:withEvent. Based on how far the touch began (x=100) and how far back is was released (for example x=30), you can apply force like this:
float forceUsed = startTouchX - endTouchX;
[_projectile.physicsBody applyForce:CGVectorMake(forceUsed, 0)];
If you are looking to angle the shot you would also have to track Y and use that variable instead of the 0 above.
As for calculating distance between 2 points on the screen, it boils down to x and y coordinates. Subtract objectA.position.x from objectB.position.x
Things can get a lot more complex of course but that all depends on what you want to do in your code. Hope this helps.
P.S. The touches above are all part of the UIResponder Class.

Resources