当有跳跃时,我如何让我的角色形象改变?

时间:2021-01-11 02:46:36

I have this code where it animates my 2d character to make it look like its running. In my touchesBegan I have this code where when theres a tap the character jumps and I want the image to change to this jump image I have for the character. Why when I tap the screen and the character jumps the image doesn't change? Thanks! Heres the code I have?

我有这个代码,它动画我的2D角色,使它看起来像它的运行。在我的touchesBegan我有这个代码,当有一个点击角色跳跃,我希望图像改变为我对该角色的这个跳跃图像。为什么当我点击屏幕并且角色跳跃图像不会改变?谢谢!继承我的代码?

    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")
    }

1 个解决方案

#1


have you tried not using the if statement? I'm not sure that your statement is returning true, therefore not going into the block, try this, without if. Just apply impulse, then change texture:

你试过不使用if语句吗?我不确定你的陈述是否正确,因此不会进入阻止,试试这个,如果没有。只需施加冲动,然后改变纹理:

theHero.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 250))
theHero.texture = SKTexture(imageNamed: "jumphero")

#1


have you tried not using the if statement? I'm not sure that your statement is returning true, therefore not going into the block, try this, without if. Just apply impulse, then change texture:

你试过不使用if语句吗?我不确定你的陈述是否正确,因此不会进入阻止,试试这个,如果没有。只需施加冲动,然后改变纹理:

theHero.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 250))
theHero.texture = SKTexture(imageNamed: "jumphero")