1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga Inc. 5 Copyright (c) 2008-2009 Jason Booth 6 7 http://www.cocos2d-x.org 8 9 Permission is hereby granted, free of charge, to any person obtaining a copy 10 of this software and associated documentation files (the "Software"), to deal 11 in the Software without restriction, including without limitation the rights 12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 copies of the Software, and to permit persons to whom the Software is 14 furnished to do so, subject to the following conditions: 15 16 The above copyright notice and this permission notice shall be included in 17 all copies or substantial portions of the Software. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 THE SOFTWARE. 26 ****************************************************************************/ 27 28 /** 29 * cc.MotionStreak manages a Ribbon based on it's motion in absolute space.<br/> 30 * You construct it with a fadeTime, minimum segment size, texture path, texture<br/> 31 * length and color. The fadeTime controls how long it takes each vertex in<br/> 32 * the streak to fade out, the minimum segment size it how many pixels the<br/> 33 * streak will move before adding a new ribbon segment, and the texture<br/> 34 * length is the how many pixels the texture is stretched across. The texture<br/> 35 * is vertically aligned along the streak segment. 36 * @class 37 * @extends cc.Node 38 */ 39 cc.MotionStreak = cc.Node.extend(/** @lends cc.MotionStreak# */{ 40 _segThreshold:0, 41 _width:0, 42 _lastLocation:cc.PointZero(), 43 44 /** 45 * @type cc.Ribbon 46 */ 47 _ribbon:null, 48 49 /** 50 * Ribbon used by MotionStreak (weak reference) 51 * @return {cc.Ribbon} 52 */ 53 getRibbon:function () { 54 return this._ribbon; 55 }, 56 57 /** 58 * @return {cc.Texture2D} 59 */ 60 getTexture:function () { 61 return this._ribbon.getTexture(); 62 }, 63 64 /** 65 * @param {cc.Texture2D} texture 66 */ 67 setTexture:function (texture) { 68 this._ribbon.setTexture(texture); 69 }, 70 71 /** 72 * @return {cc.BlendFunc} 73 */ 74 getBlendFunc:function () { 75 return this._ribbon.getBlendFunc(); 76 }, 77 78 /** 79 * @param {Number} src 80 * @param {Number} dst 81 */ 82 setBlendFunc:function (src, dst) { 83 this._ribbon.setBlendFunc(src, dst); 84 }, 85 86 /** 87 * Constructor 88 */ 89 ctor:function () { 90 this._super(); 91 }, 92 93 /** 94 * initializes a MotionStreak. The file will be loaded using the TextureMgr. 95 * @param {Number} fade 96 * @param {Number} seg 97 * @param {String} imagePath 98 * @param {Number} width 99 * @param {Number} length 100 * @param {cc.Color4B} color 101 * @return {Boolean} 102 */ 103 initWithFade:function (fade, seg, imagePath, width, length, color) { 104 this._segThreshold = seg; 105 this._width = width; 106 this._lastLocation = cc.PointZero(); 107 this._ribbon = cc.Ribbon.create(this._width, imagePath, length, color, fade); 108 this.addChild(this._ribbon); 109 110 // update ribbon position. Use schedule:interval and not scheduleUpdated. (cocos2d-iphone)issue #1075 111 this.schedule(this.update, 0); 112 return true; 113 }, 114 115 /** 116 * polling function 117 */ 118 update:function (delta) { 119 var location = this.convertToWorldSpace(cc.PointZero()); 120 this._ribbon.setPosition(cc.p(-1 * location.x, -1 * location.y)); 121 var len = cc.pLength(cc.pSub(this._lastLocation, location)); 122 if (len > this._segThreshold) { 123 this._ribbon.addPointAt(location, this._width); 124 this._lastLocation = location; 125 } 126 this._ribbon.update(delta); 127 } 128 }); 129 130 /** 131 * creates the a MotionStreak. The image will be loaded using the TextureMgr. 132 * @param {Number} fade time to fade 133 * @param {Number} seg minimum segment size 134 * @param {String} imagePath texture path 135 * @param {Number} width texture width 136 * @param {Number} length texture length 137 * @param {cc.Color4B} color color 138 * @return {cc.MotionStreak} 139 */ 140 cc.MotionStreak.create = function (fade, seg, imagePath, width, length, color) { 141 var ret = new cc.MotionStreak(); 142 if (ret && ret.initWithFade(fade, seg, imagePath, width, length, color)) { 143 return ret; 144 } 145 return null; 146 }; 147