cocos2d-js Sprite test

Javascript code posted
created at 15 Jan 01:45, updated at 23 Jan 16:42

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict'

//{{{ Imports
var util      = require('util')
  , path      = require('path')
  , cocos     = require('cocos2d')
  , geo       = require('geometry')
  , nodes     = cocos.nodes
  , actions   = cocos.actions
  , ccp       = geo.ccp

var TestCase      = require('../TestCase')
  , Director      = cocos.Director
  , Rect          = geo.Rect
  , Sprite        = nodes.Sprite
  , Sequence      = actions.Sequence
  , RepeatForever = actions.RepeatForever
  , ScaleBy       = actions.ScaleBy
  , RotateBy      = actions.RotateBy
  , Blink         = actions.Blink
  , FadeOut       = actions.FadeOut
//}}} Imports

/**
 * @class
 *
 * Example Sprite 1
 */
function Sprite1 () {
    Sprite1.superclass.constructor.call(this)

    this.isMouseEnabled = true

    var s = Director.sharedDirector.winSize
    this.addNewSprite(ccp(s.width / 2, s.height / 2))
}

Sprite1.inherit(TestCase, /** @lends Sprite1# */ {
    title: 'Sprite'
  , subtitle: 'Click screen'

  , addNewSprite: function (point) {
        var idx = Math.floor(Math.random() * 1400 / 100)
          , x = (idx % 5) * 85
          , y = (idx % 3) * 121

        var sprite = new Sprite({ file: path.join(__dirname, '../resources/grossini_dance_atlas.png')
                                , rect: new Rect(x, y, 85, 121)
                                })

        sprite.position = ccp(point.x, point.y)

        this.addChild({child: sprite
                      , z: 0
                      })

        var action
          , actionBack
          , seq
          , rand = Math.random()

        if (rand < 0.2) {
            action = new ScaleBy({ duration: 3
                                 , scale: 2
                                 })

        } else if (rand < 0.4) {
            action = new RotateBy({ duration: 3
                                  , angle: 360
                                  })

        } else if (rand < 0.6) {
            action = new Blink({ duration: 1
                               , blinks: 3
                               })

        } else if (rand < 0.8) {
            action = new RotateBy({ duration: 3
                                  , angle: 360
                                  })

            //action = new cocos.TintBy({duration:3, scale:2})
        } else {
            action = new FadeOut({ duration: 2
                                 })
        }

        actionBack = action.reverse()
        seq = new Sequence({ actions: [action, actionBack] })
        sprite.runAction(new RepeatForever(seq))
    }

  , mouseUp: function (event) {
        var location = Director.sharedDirector.convertEventToCanvas(event)
        this.addNewSprite(location)

        return true
    }
})

module.exports = Sprite1

// vim:et:st=4:fdm=marker:fdl=0:fdc=1
2.81 KB in 6 ms with coderay