SHOW:
|
|
- or go back to the newest paste.
1 | package | |
2 | { | |
3 | import flash.display.Sprite; | |
4 | import flash.display.Shape; | |
5 | import flash.display.Bitmap; | |
6 | import flash.display.BitmapData; | |
7 | import flash.geom.Rectangle; | |
8 | import flash.geom.Point; | |
9 | ||
10 | public class SpriteClass extends Sprite | |
11 | { | |
12 | public var destX:Number; | |
13 | public var destY:Number; | |
14 | public var speed:Number; | |
15 | public var remainingMoveRate:Number; | |
16 | ||
17 | public var bitmapCanvas: Bitmap; // Displays the unit | |
18 | public var bitmapDataCanvas: BitmapData; // Stores the pixels that represent the unit | |
19 | public var rectUnit: Rectangle; | |
20 | ||
21 | private var a:Number; | |
22 | private var b:Number; | |
23 | private var xDelta:Number; | |
24 | private var yDelta:Number; | |
25 | private var maxMoveSquared:Number; | |
26 | private var unitAlpha:Number; | |
27 | ||
28 | public function SpriteClass() | |
29 | { | |
30 | speed = 5.5; | |
31 | remainingMoveRate = speed; | |
32 | ||
33 | bitmapDataCanvas = new BitmapData(20, 20, true, 0x000000); | |
34 | bitmapCanvas = new Bitmap(bitmapDataCanvas); | |
35 | rectUnit = new Rectangle(0, 0, 20, 20); | |
36 | addChild(bitmapCanvas); | |
37 | } | |
38 | ||
39 | public function Update():void | |
40 | { | |
41 | MoveUnit(); | |
42 | ||
43 | bitmapDataCanvas.lock(); | |
44 | //rectUnit.x = 0; | |
45 | //rectUnit.y = 0; | |
46 | bitmapDataCanvas.fillRect(Main.rectBackground, 0x00000000); | |
47 | bitmapDataCanvas.copyPixels(Main.spriteData, rectUnit, Main.pointUnit); | |
48 | bitmapDataCanvas.unlock(); | |
49 | } | |
50 | ||
51 | public function clearDrawing(): void | |
52 | { | |
53 | bitmapDataCanvas.lock(); | |
54 | bitmapDataCanvas.copyPixels(Main.bitmapDataBackground, Main.rectBackground, Main.pointBackground); | |
55 | bitmapDataCanvas.unlock(); | |
56 | } // public function clearDrawing | |
57 | ||
58 | public function scalingUnit():void{ | |
59 | scaleX = 0.75+((y-190)/400); | |
60 | scaleY = 0.75+((y-190)/400); | |
61 | } | |
62 | ||
63 | public function MoveUnit():Boolean | |
64 | { | |
65 | a = Math.abs(destX - x); | |
66 | b = Math.abs(destY - y); | |
67 | ||
68 | if (remainingMoveRate > 1e-4) { | |
69 | // Check if the unit can reach the dest coordinates immediately. | |
70 | maxMoveSquared = a * a + b * b; | |
71 | if (maxMoveSquared <= remainingMoveRate * remainingMoveRate) { | |
72 | x = destX; | |
73 | y = destY; | |
74 | scalingUnit(); | |
75 | remainingMoveRate -= Math.sqrt(maxMoveSquared); | |
76 | return true; // Indicate that the unit has reached its dest. | |
77 | } | |
78 | ||
79 | // Determine the point where the unit will wind up in on its way | |
80 | // to (destX, destY). | |
81 | unitAlpha = Math.atan(b / a); | |
82 | xDelta = Math.cos(unitAlpha) * speed; | |
83 | yDelta = Math.sin(unitAlpha) * speed; | |
84 | destX > x ? x += xDelta : x -= xDelta; | |
85 | if(b > 1e-4){ | |
86 | destY > y ? y += yDelta : y -= yDelta; | |
87 | scalingUnit(); | |
88 | } | |
89 | } | |
90 | ||
91 | // Restore the remaining move rate for the next frame. | |
92 | remainingMoveRate = speed; | |
93 | ||
94 | // Indicate that the unit has not arrived at its dest yet. | |
95 | return false; | |
96 | } // public function move | |
97 | } | |
98 | ||
99 | } | |
100 | ||
101 | package | |
102 | { | |
103 | import flash.display.Sprite; | |
104 | import flash.events.Event; | |
105 | import flash.display.Bitmap; | |
106 | import flash.display.BitmapData; | |
107 | import flash.geom.Point; | |
108 | import flash.geom.Rectangle; | |
109 | ||
110 | public class Main extends Sprite | |
111 | { | |
112 | [Embed(source="../Images/Test.png")] | |
113 | private var test:Class; | |
114 | private var spriteSheet: Bitmap; | |
115 | ||
116 | public static var spriteData: BitmapData; | |
117 | public static var pointUnit: Point = new Point(0,0); | |
118 | public static var pointBackground: Point = new Point(0,0); | |
119 | public static var rectBackground: Rectangle = new Rectangle(0, 0, 20, 20); | |
120 | public static var bitmapDataBackground: BitmapData = new BitmapData(20, 20, true, 0x000000); | |
121 | ||
122 | [Embed(source="../Images/Background.png")] | |
123 | private var background:Class; | |
124 | private var bgBit:Bitmap; | |
125 | ||
126 | private var stats:Stats; | |
127 | private var sprites:Vector.<SpriteClass>; | |
128 | private var sprite:SpriteClass; | |
129 | ||
130 | private var i:int; | |
131 | - | private var count:int = 40; //160 |
131 | + | private var count:int = 88; //160 |
132 | private var frameCount:int; | |
133 | ||
134 | public function Main():void | |
135 | { | |
136 | bgBit = new background(); | |
137 | bgBit.y += 100; | |
138 | addChild(bgBit); | |
139 | stats = new Stats(); | |
140 | addChild(stats); | |
141 | ||
142 | spriteSheet = new test(); | |
143 | spriteData = spriteSheet.bitmapData; | |
144 | sprites = new Vector.<SpriteClass>(); | |
145 | for (i = 0; i < count; i++) | |
146 | { | |
147 | sprite = new SpriteClass(); | |
148 | - | sprite.destX = 350 + 10*(i%4); |
148 | + | sprite.destX = 350 + 20*(i%4); |
149 | - | sprite.destY = 350 + 10*(int(Math.random()*4)); |
149 | + | sprite.destY = 350; |
150 | sprite.x = int(Math.random() * 700); | |
151 | sprite.y = 290 + int(Math.random() * 200); | |
152 | addChild(sprite); | |
153 | sprites.push(sprite); | |
154 | } | |
155 | ||
156 | frameCount = 0; | |
157 | addEventListener(Event.ENTER_FRAME, MainEngine); | |
158 | } | |
159 | ||
160 | private function MainEngine(e:Event):void | |
161 | { | |
162 | for (i = 0; i < count; i++) sprites[i].Update(); | |
163 | frameCount++; | |
164 | } | |
165 | ||
166 | } | |
167 | ||
168 | } |