Ghost = SKSpriteNode(imageNamed: "Ghost1")
Ghost.size = CGSize(width: 50, height: 50)
Ghost.position = CGPoint(x: self.frame.width / 2 - Ghost.frame.width, y: self.frame.height / 2)
Ghost.physicsBody = SKPhysicsBody(circleOfRadius: Ghost.frame.height / 1.4)
Ghost.physicsBody?.categoryBitMask = PhysicsCatagory.Ghost
Ghost.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall
Ghost.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.Score
Ghost.physicsBody?.affectedByGravity = false
Ghost.physicsBody?.isDynamic = true
Ghost.zPosition = 2
self.addChild(Ghost)在我的应用程序上,我有一个在屏幕上到处移动的对象,它的名字是“幽灵”。我不知道如何设置一个可以更改代码的按钮
Ghost = SKSpriteNode(imageNamed: "Ghost2")而不是
Ghost = SKSpriteNode(imageNamed: "Ghost1")发布于 2016-09-24 11:05:07
要更改SKSpriteNode的图像,请指定不同的纹理:
Ghost.texture = SKTexture(imageNamed:"Ghost2")注意:变量名应该使用小写字母,以区别于类名。
一个包含Button和Ghost的示例实现如下图所示,右上角创建了一个红色按钮。注意按钮和重影的声明现在发生在didMoveToView之外,这样以后当用户点击屏幕时就可以引用这些变量。
class ButtonGhostScene: SKScene {
var button: SKNode! = nil
var ghost: SKSpriteNode! = nil
override func didMove(to view: SKView) {
button = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 100, height: 44))
button.position = CGPoint(x:self.size.width, y:self.size.height)
ghost = SKSpriteNode(imageNamed: "Ghost1")
ghost.size = CGSize(width: 50, height: 50)
ghost.position = CGPoint(x: self.frame.width / 2 - Ghost.frame.width, y: self.frame.height / 2)
ghost.physicsBody = SKPhysicsBody(circleOfRadius: Ghost.frame.height / 1.4)
ghost.physicsBody?.categoryBitMask = PhysicsCatagory.Ghost
ghost.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall
ghost.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.Score
ghost.physicsBody?.affectedByGravity = false
ghost.physicsBody?.isDynamic = true
ghost.zPosition = 2
self.addChild(ghost)
self.addChild(button)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// Loop over all the touches in this event
for touch: AnyObject in touches {
// Get the location of the touch in this scene
let location = touch.location(in: self)
// Check if the location of the touch is within the button's bounds
if button.containsPoint(location) {
ghost.texture = SKTexture(imageNamed:"Ghost2")
}
}
}
}当用户点击屏幕时,将执行touchesBegan,并进行检查以查看用户是否点击了按钮。
https://stackoverflow.com/questions/39671992
复制相似问题