万年素人からHackerへの道

万年素人がHackerになれるまで殴り書きするぜ。

今後のコピペ用PlayerシステムCoffeeScript cocos2d-JS

coco2d-JSで今後コピペするため

CoffeeScript

GRAVITY = 8.8
# Player
Player = cc.Sprite.extend(
    jump_speed: 50
    move_speed: 3
    x: 0
    y: 0
    vx: 0
    vy: 0
    is_ground: true 
    jump_y: 0
    
    ctor: () ->
        this._super()
        this.initWithFile(res.CloseSelected_png)
    setDirection: (keyboardArrows) ->
        side = (if keyboardArrows.right then 1 else 0) + (if keyboardArrows.left then -1 else 0)
        
        # ここに書かないとだめ
        upDown = 0

        if this.is_ground 
            upDown = (if keyboardArrows.up then 1 else 0) + (if keyboardArrows.down then -1 else 0)
        else
            upDown -= 0.1

        this.vx += side * this.move_speed
        this.vy += upDown * this.move_speed
        #console.log (side)
        #console.log (upDown)
    move: () ->
        this.x = this.getPosition().x
        this.y = this.getPosition().y

        # 減速
        this.vx *= 0.9
        this.vy *= 0.9
        
        if Math.abs(this.vx) < 0.01
            this.vx = 0
        if Math.abs(this.vy) < 0.01
            this.vy = 0

        this.x += this.vx
        if this.is_ground
            this.y += this.vy
        else
            if this.y + this.vy < this.jump_y
                this.y = this.jump_y
                this.vy = 0
                this.is_ground = true
            else
                this.vy -= GRAVITY
                this.y += this.vy
 
        this.setPosition(this.x, this.y)
    jump: () ->
        if this.is_ground 
            #console.log('jump')
            this.jump_y = this.y
            this.vy = this.jump_speed 
            this.is_ground = false
)