I am creating an Iphone game in Xcode using swift , and there is a ball i want it to keep constantly rotating , i think i need to do it with the update method , but how could i do it , and if there another way with out using the update method please let me know
You could use an SKAction (take a look at the documentation; you'll see SKActions have a variety of uses). For a rotation you could do:
// The angle is measured in radians and the duration is measured in seconds.
let rotateAction = SKAction.rotateByAngle(1, duration: 1)
To make the action run forever you can use another SKAction:
let repeatAction = SKAction.repeatActionForever(rotateAction)
Finally, you need to make your node (ball) run the action:
ball.runAction(repeatAction)
Take a look at the SKNode documentation for other methods you can use to run an SKAction.
Also, I'm assuming you're new to Sprite Kit, so I'd recommend you take a look at The Sprite Kit Programming Guide.
Related
using ARKit and SceneKit i'm try to make flying a Drone model.
for now successfully I can place the drone over a surface, simulate collision end detect contact between the base and the drone.
Now I would like to try to fly this drone, apply some constant force!
(I don't want to simply move the Y axis to move up the drone) but I want to simulate a force pulling it up like it happen in the real life.
I started to write some code, and call this method once I press the up button..
// hit test and find object with specific name
func applyForceToDrone() {
print("tapped apply force drone at position ")
let nodo = arrayDrone[0] // return the scnNode of the Drone in the scene
let force = SCNVector3(0, 15, 0)
print("appply force")
nodo.physicsBody?.applyForce(force, asImpulse: false)
}
resulting of this code is the drone jumping up and down every time I press the up button.
but this force need to be apply constant in the time ...
How can I constant apply a force to keep the drone up in the air? any tips how I can approach this problem ...
I can't find any example where a constant force is apply to an object.
thanks for the help.
You have to apply force inside one of loop functions. To do so, define a variable var applyForce=false. Then on for example buttonUp touch begin, set applyForce=true, on touch end set applyForce=false. Then inside loop function : if applyForce then do apply force. You have to tweak amount of force. Also you can add more parameters.
I created a RealityKit project which loads objects (usdz files). Using the LiDAR is really great for occlusion and be able to see the real world mesh.
I would like to use something found in the Apple's documentation : Perspective Camera. If I well understood this could be compare as a third person camera.
I created a dedicated button in my arView which, when called, execute the code following :
let cameraEntity = PerspectiveCamera()
cameraEntity.camera.far = 10
cameraEntity.camera.fieldOfViewInDegrees = 60
cameraEntity.camera.near = 0.01
let cameraAnchor = AnchorEntity(world: .zero)
cameraAnchor.children.append(cameraEntity)
self.arView.scene.anchors.append(cameraAnchor)
When is code is called the the become black.... I do not understand how to place the camera to see the scanned mesh.
If someone has an idea ? Thanks in advance!
It depends where the USDZ you're looking at is located. I think the default will mean the camera is located at the origin looking in the direction of [0, 0, -1].
You could change this using the Entity.look(at:from:upVector:relativeTo:) method. Making sure that your from: parameter is far enough from the centre of your USDZ object.
Creating a game like Doodle Jump where my player is constantly being moved up by bouncing off of obstacles. Ive tried every trick in the book but nothing seems to be working/doing exactly what I want. Can anyone give me some tips?
iOS 9 introduced the Camera Node. Use SKCameraNode, which is a subclass of SKNode, and can be translated and rotated in same way.
So, instead of moving all of your background elements in the opposite direction of your hero/player, you can simply attach your scene's camera node to your hero/player and the rest is taken care of.
PS. You can also do cool stuff like scaling the camera size.
EDIT.
Happy to include an example.
First, make a camera constant in your scene.
import SpriteKit
class myFirstScene: SKScene {
let myCamera: SKCameraNode = SKCameraNode()
...
}
Then in your didMoveToView() function, assign the scene's built-in camera variable to the camera constant we made earlier.
override func didMoveToView( view: SKView ) {
camera = myCamera
...
}
Now, there are a few different ways to "attach" your camera to your hero/player. The first is to attach your camera node to your hero.
hero.addChild( myCamera )
I don't even know if it works that easily because my game uses something different, a simpler version is below.
update(){
camera!.zRotation = hero.zRotation
camera!.position = hero.position
}
If I'm making a game in SpriteKit that has a large "world", and I need the user to have the option of zooming in and out of the SKScene, how would I go about this? Or, to make things simpler, in the didMoveToView function, how can I present more of the world to the user's device's screen (without using world.runAction(SKAction.scaleTo(0.5)) or something)?
There's a SKCameraNode that's built specifically for this. The SKCameraNode defines the viewport into your scene. You create a camera node and assign it to the camera property of your scene.
let cameraNode = SKCameraNode()
cameraNode.position = CGPoint(x: scene.size.width / 2, scene.size.height / 2)
scene.addChild(cameraNode)
scene.camera = cameraNode
You can then create actions and run those actions on the camera. So to zoom in on the scene, you'd do this.
let zoomInAction = SKAction.scale(to: 0.5, duration: 1)
cameraNode.run(zoomInAction)
The cameraNode basically is a square node in the scene, that I think takes the proportions of the view by default? Cuz there's no size initializer. So when you make it smaller, the scene looks like it gets zoomed. To zoom out you'd make an action that increases the scale. Basically imagine a rectangle on your entire scene, and whatever is in the cameraNode's rectangle directly shows on your iPhone screen. You can also add moveTo actions and sequence actions and set timingModes on the actions same as if it were your normal spriteNode.
Here's the WWDC where the apple guy shows what I've just said. CameraNode bit is around 3 mins before the end.
https://developer.apple.com/videos/play/wwdc2015-604/
So, the best solution I could could find goes something like this. In the didMoveToView function, create an SKSpriteNode called World and make it whatever size you want your world to be. Then, write world.setScale(0.5) if you want a 50% zoom-out. However, if you have a player node that needs to always be centered in the screen, you'll need to add the following to your update function.
override func update(currentTime: CFTimeInterval) {
world.position.x = -player.position.x * (0.5)
world.position.y = -player.position.y * (0.5)
}
I am currently moving a rocket using this code:
let location = touch.locationInNode(self)
rocket.position.y = location.y
I want to know if it would be easier to use the SKActionTimingMode.EaseOut and if so, how do I make the rocket move up and down along the Y-axis using an SKAction.
Is there a way to smoothly move a node with the code I am already using?
The app is set to landscape.
Here's how you can do it with SKAction. I think using SKAction would be much smoother. Alternatively, you could also attach a physics body to the node and call applyForce to it by calculating the force of the swipe or if its a touch then calling applyImpulse with a predefined value depending on where the touch occurred (i.e. above or below the object.)
let location = touch.locationInNode(self)
let action = SKAction.moveToY(location.y, duration: 3.0) // Or however much time you want to the action to run.
action.timingMode = .EaseInEaseOut
rocket.runAction(action)