Create SKSpriteNode with an Asset Programmatically
It seems like you need to explicitly set the size too:
sprite.texture = SKTexture(imageNamed: "myImage1")
sprite.size = sprite.texture!.size()
..or you can specify the image in init:
let sprite = SKSpriteNode(imageNamed: "myImage1")
Getting the filename of an SKSpriteNode in Swift 3
It is stored in the description, so here is a nice little extension I made to rip it out.
extension SKTexture
{
var name : String
{
return self.description.slice(start: "'",to: "'")!
}
}
extension String {
func slice(start: String, to: String) -> String?
{
return (range(of: start)?.upperBound).flatMap
{
sInd in
(range(of: to, range: sInd..<endIndex)?.lowerBound).map
{
eInd in
substring(with:sInd..<eInd)
}
}
}
}
usage:
print(sprite.texture!.name)
Asset catalog image slicing in Spritekit
If centeRect, yScale, xScale are set the button will streach properly
let sprite = SKSpriteNode(texture: spriteTexture, color: nil, size: CGSizeMake(290, 58));
sprite.position = CGPointMake(0, -100);
sprite.centerRect = CGRectMake( 140/290.0, 24/58.0, 10.0/58.0, 10.0/58.0);
sprite.yScale = CGFloat(250.0 / 58.0);
sprite.xScale = CGFloat((self.frame.size.width - 20) / 290.0);
self.addChild(sprite);
Turning Object with SkSpriteNode
You could apply a force of an angular type, so as to rotate the body, and turn off damping so it continues to rotate forever:
https://developer.apple.com/reference/spritekit/skaction/1417775-applyangularimpulse
Or, apply torque:
Firstly, make sure it’s able to rotate by setting this to true:
https://developer.apple.com/reference/spritekit/skphysicsbody/1519986-allowsrotation
Make sure there’s no angular damping so that it doesn’t slow in rotation rate:
https://developer.apple.com/reference/spritekit/skphysicsbody/1519913-angulardamping
Now make it spin by applying a torque:
https://developer.apple.com/reference/spritekit/skphysicsbody/1519588-applytorque
Or, set a spin rate:
https://developer.apple.com/reference/spritekit/skphysicsbody/1519766-angularvelocity
Move SKSpriteNode by Y - Swift 2 + SpriteKit
You need to run the moveAndRemove
action on each spaceship. I suggest you move the action definition to the createSpaceship
method and run the moveAndRemove
action on each spaceship that you create. For example,
func createSpaceship() {
// This can be a local variable and combined into a single statement
let spaceship = SKSpriteNode(imageNamed: "Spaceship")
spaceship.size = CGSize(width: 70, height: 100)
spaceship.setScale(1.0)
spaceship.position = CGPoint(x: screenSize.width / 3, y: screenSize.height - 75)
spaceship.zPosition = 1
let distance = CGFloat(screenSize.height + spaceship.frame.height)
let moveTargets = SKAction.moveToY(spaceship.frame.origin.y - distance, duration: 8.0)
let removeTargets = SKAction.removeFromParent()
let moveAndRemove = SKAction.sequence([moveTargets,removeTargets])
// Add the action to the list of actions executed by the spaceship
spaceship.runAction(moveAndRemove)
self.addChild(spaceship)
}
How to programmatically assign a material to a 3D SCNNode?
Overall, I was setting the materials of an object like this and it was working (to only grab one face of the box
var imageMaterial = SCNMaterial()
imageMaterial.isDoubleSided = false
imageMaterial.diffuse.contents = UIImage(named: "myImage")
var cube: SCNGeometry? = SCNBox(width: 1.0, height: 1.0, length: 1, chamferRadius: 0)
var node = SCNNode(geometry: cube)
node.geometry?.materials = [imageMaterial]
So it could possibly be that you haven't been able to grab the object, as stated in the comments.
Related Topics
How to Remove Items from an Array When Deselecting a Row in a UItableview
Swift 3 Cocoa: Use Quicklook to Preview File in Os X
Nstimer Scheduledtimerwithtimeinterval and Target Is "Class Level Function"
Realm + Nstableview + Nsarraycontroller
Can Not Import Tensorflow for Swift in Xcode Playground
How to Move an Object Towards a Direction Without Stopping
Wrong Value Returned After Formatting Timestamp
How to Insert UIlabel After UItextfield in UIalertcontroller
Cloudkit - What to Do When a User Adds, Modifies or Deletes an Object While Offline
Cannot Assign Value of Type 'Menuview' to Type 'some View'
Arkit: Place Object on a Plane Doesn't Work Properly
How to Assign Elements of a Dictionary to JSON Object in Vapor 3