Skip to content

Commit 43014f9

Browse files
author
WanderWang
committedDec 23, 2014
添加MoveBy
1 parent b95749b commit 43014f9

File tree

7 files changed

+222
-6
lines changed

7 files changed

+222
-6
lines changed
 

‎action.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "action",
3+
"dependence": ["core"],
4+
"source":"src/action/",
5+
"file_list": [
6+
"Manager.ts",
7+
"Action.ts",
8+
"ActionInterval.ts",
9+
"Move.ts"
10+
]
11+
}

‎egretProperties.json

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"modules": [
44
{
55
"name": "core"
6+
},
7+
{
8+
"name": "action",
9+
"path":"."
610
}
711
],
812
"native": {

‎src/Main.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class Main extends egret.DisplayObjectContainer{
8181
*/
8282
private createGameScene():void{
8383

84-
new egret.action.Manager();
8584

8685
var sky:egret.Bitmap = this.createBitmapByName("bgImage");
8786
this.addChild(sky);
@@ -90,6 +89,16 @@ class Main extends egret.DisplayObjectContainer{
9089
sky.width = stageW;
9190
sky.height = stageH;
9291

92+
93+
var manager = new egret.action.Manager();
94+
var action = egret.action.MoveBy.create(1,cc.p(100,100));
95+
manager.addAction(action, sky, false);
96+
97+
98+
egret.Ticker.getInstance().register(function(dt){
99+
manager.update(dt / 1000);
100+
},this)
101+
93102
var topMask:egret.Shape = new egret.Shape();
94103
topMask.graphics.beginFill(0x000000, 0.5);
95104
topMask.graphics.drawRect(0, 0, stageW, stageH);

‎src/action/Action.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module egret.action {
1313

1414

1515
private originalTarget:any;
16-
private target:any;
16+
public target:any;
1717
private tag:number;
1818

1919
constructor() {
@@ -22,6 +22,14 @@ module egret.action {
2222
this.tag = ACTION_TAG_INVALID;
2323
}
2424

25+
/**
26+
*
27+
* @return {cc.Node}
28+
*/
29+
public getOriginalTarget() {
30+
return this.originalTarget;
31+
}
32+
2533

2634
/**
2735
* to copy object with deep copy.
@@ -85,11 +93,10 @@ module egret.action {
8593

8694
}
8795

88-
8996
}
9097

9198

92-
export class FiniteTimeAction {
99+
export class FiniteTimeAction extends Action {
93100

94101

95102
public _duration:number = 0;

‎src/action/ActionInterval.ts

+88
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,91 @@
11
/**
22
* Created by wander on 14-12-22.
33
*/
4+
module egret.action {
5+
6+
7+
var FLT_EPSILON = 0.0000001192092896;
8+
9+
export class ActionInterval extends FiniteTimeAction {
10+
11+
private _speed:number;
12+
private _times:number;
13+
private _repeatForever:boolean;
14+
private MAX_VALUE:number;
15+
private _repeatMethod:boolean;
16+
private _speedMethod:boolean;
17+
private _elapsed:number;
18+
private _firstTick:boolean;
19+
20+
constructor() {
21+
super();
22+
this._elapsed = 0;
23+
this._firstTick = false;
24+
}
25+
26+
27+
/**
28+
* Initializes the action.
29+
* @param {Number} d duration in seconds
30+
* @return {Boolean}
31+
*/
32+
public initWithDuration(d,param) {//todo
33+
this._duration = (d === 0) ? FLT_EPSILON : d;
34+
// prevent division by 0
35+
// This comparison could be in step:, but it might decrease the performance
36+
// by 3% in heavy based action games.
37+
this._elapsed = 0;
38+
this._firstTick = true;
39+
// return true;
40+
}
41+
42+
43+
/**
44+
* Returns true if the action has finished.
45+
* @return {Boolean}
46+
*/
47+
public isDone() {
48+
return (this._elapsed >= this._duration);
49+
}
50+
51+
52+
53+
/**
54+
* called every frame with it's delta time. <br />
55+
* DON'T override unless you know what you are doing.
56+
*
57+
* @param {Number} dt
58+
*/
59+
public step (dt) {
60+
if (this._firstTick) {
61+
this._firstTick = false;
62+
this._elapsed = 0;
63+
} else
64+
this._elapsed += dt;
65+
66+
//this.update((1 > (this._elapsed / this._duration)) ? this._elapsed / this._duration : 1);
67+
//this.update(Math.max(0, Math.min(1, this._elapsed / Math.max(this._duration, cc.FLT_EPSILON))));
68+
var t = this._elapsed / (this._duration > 0.0000001192092896 ? this._duration : 0.0000001192092896);
69+
t = (1 > t ? t : 1);
70+
this.update(t > 0 ? t : 0);
71+
72+
//Compatible with repeat class, Discard after can be deleted (this._repeatMethod)
73+
if(this._repeatMethod && this._times > 1 && this.isDone()){
74+
if(!this._repeatForever){
75+
this._times--;
76+
}
77+
//var diff = locInnerAction.getElapsed() - locInnerAction._duration;
78+
this.startWithTarget(this.target);
79+
// to prevent jerk. issue #390 ,1247
80+
//this._innerAction.step(0);
81+
//this._innerAction.step(diff);
82+
this.step(this._elapsed - this._duration);
83+
84+
}
85+
}
86+
87+
88+
}
89+
90+
91+
}

‎src/action/Manager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ module egret.action {
7373
/**
7474
* @param {Number} dt delta time in seconds
7575
*/
76-
private update(dt) {
76+
public update(dt) {
7777
var locTargets = this._arrayTargets , locCurrTarget;
7878
for (var elt = 0; elt < locTargets.length; elt++) {
7979
this._currentTarget = locTargets[elt];
@@ -121,7 +121,7 @@ module egret.action {
121121
/** Removes an action given an action reference.
122122
* @param {cc.Action} action
123123
*/
124-
public removeAction(action) {
124+
public removeAction(action:Action) {
125125
// explicit null handling
126126
if (action == null)
127127
return;

‎src/action/Move.ts

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Created by wander on 14-12-22.
3+
*/
4+
module egret.action {
5+
6+
export class MoveBy extends ActionInterval {
7+
8+
9+
private _positionDelta:cc.Point;
10+
private _startPosition:cc.Point;
11+
private _previousPosition:cc.Point;
12+
13+
14+
constructor() {
15+
16+
super();
17+
18+
this._positionDelta = cc.p(0, 0);
19+
this._startPosition = cc.p(0, 0);
20+
this._previousPosition = cc.p(0, 0);
21+
}
22+
23+
public initWithDuration(duration:number, position) {
24+
super.initWithDuration(duration,position);
25+
this._positionDelta.x = position.x;
26+
this._positionDelta.y = position.y;
27+
28+
29+
}
30+
31+
/**
32+
* @param {Number} target
33+
*/
34+
public startWithTarget(target:DisplayObject) {
35+
super.startWithTarget(target);
36+
var locPosX = target.x;
37+
var locPosY = target.y;
38+
this._previousPosition.x = locPosX;
39+
this._previousPosition.y = locPosY;
40+
this._startPosition.x = locPosX;
41+
this._startPosition.y = locPosY;
42+
}
43+
44+
45+
/**
46+
* @param {Number} time time in seconds
47+
*/
48+
public update(time) {
49+
if (this.target) {
50+
var x = this._positionDelta.x * time;
51+
var y = this._positionDelta.y * time;
52+
var locStartPosition = this._startPosition;
53+
if (false) {//cc.ENABLE_STACKABLE_ACTIONS) { todo
54+
// var targetX = this._target.getPositionX();
55+
// var targetY = this._target.getPositionY();
56+
// var locPreviousPosition = this._previousPosition;
57+
//
58+
// locStartPosition.x = locStartPosition.x + targetX - locPreviousPosition.x;
59+
// locStartPosition.y = locStartPosition.y + targetY - locPreviousPosition.y;
60+
// x = x + locStartPosition.x;
61+
// y = y + locStartPosition.y;
62+
//
63+
// this._target.setPosition(x, y);
64+
// locPreviousPosition.x = x;
65+
// locPreviousPosition.y = y;
66+
} else {
67+
this.target.x = locStartPosition.x + x;
68+
this.target.y = locStartPosition.y + y;
69+
}
70+
}
71+
}
72+
73+
74+
public static create(duration, deltaPosition) {
75+
var moveBy = new MoveBy();
76+
moveBy.initWithDuration(duration, deltaPosition);
77+
return moveBy;
78+
}
79+
}
80+
81+
82+
}
83+
84+
module cc {
85+
86+
export function p(x:number, y:number):Point {
87+
return {x: x, y: y};
88+
}
89+
90+
export interface Point {
91+
92+
x:number;
93+
94+
y:number;
95+
}
96+
97+
}

0 commit comments

Comments
 (0)
Please sign in to comment.