Swift 当出现跳转时,我将如何更改角色图像?
我有这段代码,它可以为我的2d角色设置动画,使其看起来像是在运行。在我的touchestarted中,我有一个代码,当点击时,角色会跳转,我希望图像更改为角色的跳转图像。为什么当我点击屏幕,角色跳跃时,图像没有改变?谢谢这是我的密码Swift 当出现跳转时,我将如何更改角色图像?,swift,animation,Swift,Animation,我有这段代码,它可以为我的2d角色设置动画,使其看起来像是在运行。在我的touchestarted中,我有一个代码,当点击时,角色会跳转,我希望图像更改为角色的跳转图像。为什么当我点击屏幕,角色跳跃时,图像没有改变?谢谢这是我的密码 func addHero() { let heroTextureOne = SKTexture(imageNamed: "heroup") let heroTextureTwo = SKTexture(imageNamed: "herodo
func addHero() {
let heroTextureOne = SKTexture(imageNamed: "heroup")
let heroTextureTwo = SKTexture(imageNamed: "herodown")
let anim = SKAction.animateWithTextures([heroTextureOne, heroTextureTwo], timePerFrame: 0.2)
let run = SKAction.repeatActionForever(anim)
theHero = SKSpriteNode(texture: heroTextureTwo)
theHero.runAction(run)
theHero.physicsBody = SKPhysicsBody(rectangleOfSize: theHero.size)
theHero.physicsBody?.affectedByGravity = true
theHero.position = CGPointMake(self.size.width / 3, self.size.height / 1.0)
theHero.zPosition = 15
addChild(theHero)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch: UITouch = touches.first as! UITouch
var location = touch.locationInNode(self)
var node = self.nodeAtPoint(location)
if (theHero.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 250)) != nil) {
theHero.texture = SKTexture(imageNamed: "jumphero")
}
func addHero(){
让heroTextureOne=SKTexture(图像名为:“heroup”)
让HeroTextureWO=SKTexture(图像名为:“herodown”)
让anim=SKAction.animateWithTextures([heroTextureOne,HeroTextureTo],timePerFrame:0.2)
让运行=SKAction.repeatActionForever(动画)
theHero=SKSpriteNode(纹理:HeroTextureTo)
英雄。奔跑动作(奔跑)
theHero.physicsBody=SKPhysicsBody(矩形尺寸:theHero.size)
英雄。物理身体?受重力影响=真
theHero.position=CGPointMake(self.size.width/3,self.size.height/1.0)
theHero.zPosition=15
addChild(英雄)
}
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
var touch:UITouch=touch.first as!UITouch
var位置=触摸位置节点(自身)
var node=self.nodeAtPoint(位置)
if(theHero.physicsBody?.applyImpulse(CGVector(dx:0,dy:250))!=nil){
theHero.texture=SKTexture(图像名为:“jumphero”)
}
您是否尝试过不使用if语句?我不确定您的语句是否返回true,因此不进入块,尝试此操作,不使用if。只需应用冲动,然后更改纹理:
theHero.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 250))
theHero.texture = SKTexture(imageNamed: "jumphero")
试着把它去掉,只在一行上施加一个脉冲,在另一行上改变纹理你确定你的项目中有一个名为“jumphero”的图像吗?不,我只是试着在Xcode中做同样的事情,它对我有效,同样的代码(显然不同的图像)你把HERO变量声明为什么?即使在我点击屏幕的时候,跑步的动画仍在继续。我想这与此有关。