Poruszająca się perpektywa w trzech wymiarach 3D [ ActionScript 3 ]
Skrypt tworzy las drzew, przez który możemy wjechać, oddalić podwyższyć swój tor lotu, przejść na prawo/lewo.
Skrypt reaguje na strzałki oraz spację.
Wersja On-line:
Listing
//KOD DLA KLASY Perspektywa3D.as
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
[SWF(backgroundColor=0x000000)]
public class Perspektywa3D extends Sprite
{
private var drzewa:Array;
private var iloscDrzew:uint = 100;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
private var floor:Number = 200;
private var ax:Number = 0;
private var ay:Number = 0;
private var az:Number = 0;
private var vx:Number = 0;
private var vy:Number = 0;
private var vz:Number = 0;
private var grawitacja:Number = 0.3;
private var trcie:Number = 0.98;
public function Perspektywa3D()
{
init();
}
private function init():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
drzewa = new Array();
for(var i:uint = 0; i < iloscDrzew; i++)
{
var tree:Tree = new Tree();
drzewa.push(tree);
tree.xpos = Math.random() * 2000 - 1000;
tree.ypos = floor;
tree.zpos = Math.random() * 10000;
addChild(tree);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onEnterFrame(event:Event):void
{
vx += ax;
vy += ay;
vz += az;
vy -= grawitacja;
for(var i:uint = 0; i < iloscDrzew; i++)
{
var tree:Tree = drzewa[i];
move(tree);
}
vx *= trcie;
vy *= trcie;
vz *= trcie;
sortZ();
}
private function onKeyDown(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.UP :
az = -1;
break;
case Keyboard.DOWN :
az = 1;
break;
case Keyboard.LEFT :
ax = 1;
break;
case Keyboard.RIGHT :
ax = -1;
break;
case Keyboard.SPACE :
ay = 1;
break;
default :
break;
}
}
private function onKeyUp(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.UP :
case Keyboard.DOWN :
az = 0;
break;
case Keyboard.LEFT :
case Keyboard.RIGHT :
ax = 0;
break;
case Keyboard.SPACE :
ay = 0;
break;
default :
break;
}
}
private function move(tree:Tree):void
{
tree.xpos += vx;
tree.ypos += vy;
tree.zpos += vz;
if(tree.ypos < floor)
{
tree.ypos = floor;
}
if(tree.zpos < -fl)
{
tree.zpos += 10000;
}
if(tree.zpos > 10000 - fl)
{
tree.zpos -= 10000;
}
var scale:Number = fl / (fl + tree.zpos);
tree.scaleX = tree.scaleY = scale;
tree.x = vpX + tree.xpos * scale;
tree.y = vpY + tree.ypos * scale;
tree.alpha = scale;
}
private function sortZ():void
{
drzewa.sortOn("zpos", Array.DESCENDING | Array.NUMERIC);
for(var i:uint = 0; i < iloscDrzew; i++)
{
var tree:Tree = drzewa[i];
setChildIndex(tree, i);
}
}
}
}
//KOD DLA KLASY Pilka.as
package {
import flash.display.Sprite;
public class Pilka extends Sprite {
public var radius:Number;
private var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public var mass:Number = 1;
public function Pilka(radius:Number=40, color:uint=0xff0000) {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
[SWF(backgroundColor=0x000000)]
public class Perspektywa3D extends Sprite
{
private var drzewa:Array;
private var iloscDrzew:uint = 100;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
private var floor:Number = 200;
private var ax:Number = 0;
private var ay:Number = 0;
private var az:Number = 0;
private var vx:Number = 0;
private var vy:Number = 0;
private var vz:Number = 0;
private var grawitacja:Number = 0.3;
private var trcie:Number = 0.98;
public function Perspektywa3D()
{
init();
}
private function init():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
drzewa = new Array();
for(var i:uint = 0; i < iloscDrzew; i++)
{
var tree:Tree = new Tree();
drzewa.push(tree);
tree.xpos = Math.random() * 2000 - 1000;
tree.ypos = floor;
tree.zpos = Math.random() * 10000;
addChild(tree);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onEnterFrame(event:Event):void
{
vx += ax;
vy += ay;
vz += az;
vy -= grawitacja;
for(var i:uint = 0; i < iloscDrzew; i++)
{
var tree:Tree = drzewa[i];
move(tree);
}
vx *= trcie;
vy *= trcie;
vz *= trcie;
sortZ();
}
private function onKeyDown(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.UP :
az = -1;
break;
case Keyboard.DOWN :
az = 1;
break;
case Keyboard.LEFT :
ax = 1;
break;
case Keyboard.RIGHT :
ax = -1;
break;
case Keyboard.SPACE :
ay = 1;
break;
default :
break;
}
}
private function onKeyUp(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.UP :
case Keyboard.DOWN :
az = 0;
break;
case Keyboard.LEFT :
case Keyboard.RIGHT :
ax = 0;
break;
case Keyboard.SPACE :
ay = 0;
break;
default :
break;
}
}
private function move(tree:Tree):void
{
tree.xpos += vx;
tree.ypos += vy;
tree.zpos += vz;
if(tree.ypos < floor)
{
tree.ypos = floor;
}
if(tree.zpos < -fl)
{
tree.zpos += 10000;
}
if(tree.zpos > 10000 - fl)
{
tree.zpos -= 10000;
}
var scale:Number = fl / (fl + tree.zpos);
tree.scaleX = tree.scaleY = scale;
tree.x = vpX + tree.xpos * scale;
tree.y = vpY + tree.ypos * scale;
tree.alpha = scale;
}
private function sortZ():void
{
drzewa.sortOn("zpos", Array.DESCENDING | Array.NUMERIC);
for(var i:uint = 0; i < iloscDrzew; i++)
{
var tree:Tree = drzewa[i];
setChildIndex(tree, i);
}
}
}
}
//KOD DLA KLASY Pilka.as
package {
import flash.display.Sprite;
public class Pilka extends Sprite {
public var radius:Number;
private var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public var mass:Number = 1;
public function Pilka(radius:Number=40, color:uint=0xff0000) {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}
Dodano przez: divix
Ranga: Administrator serwisu Punktów: 0
Ranga: Administrator serwisu Punktów: 0
Komentarze użytkowników
:: Losowe artykuły
:: Wymiana linków
Modowe inspiracje |
Android Gry i Aplikacje |
ZaplanujTransport.pl: Przeprowadzki, transport, aukcje |
Logo dla firmy |
Change Tires - Car Weather Forecast Reminder |
Laminas: MVC Framework for PHP |
IT Books Reviews and Programming: JS, JAVA, PHP, ANDROID, CSS |
Katalog roślin |
Programming articles: JAVA, PHP, C++, Python, JavaScript |
Kancelaria Adwokacka Łukasz Huszno