Odbicia obiektu piłki w trój wymiarze - bounce 3D [ ActionScript 3 ]
Skrypt pokazuje jak stworzyć efekt odbicia piłki w trzech wymiarach.
Wersja On-line:
Listing
//KOD DLA KLASY Odbicie3D.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;
public class Odbicie3D extends Sprite
{
private var pilka:Pilka;
private var xpos:Number = 0;
private var ypos:Number = 0;
private var zpos:Number = 0;
private var vx:Number = Math.random() * 10 - 5;
private var vy:Number = Math.random() * 10 - 5;
private var vz:Number = Math.random() * 10 - 5;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
private var top:Number = -100;
private var dol:Number = 100;
private var lewo:Number = -100;
private var prawo:Number = 100;
private var przod:Number = 100;
private var tyl:Number = -100;
public function Odbicie3D()
{
init();
}
private function init():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
pilka = new Pilka(15);
addChild(pilka);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
xpos += vx;
ypos += vy;
zpos += vz;
var radius:Number = pilka.radius;
if(xpos + radius > prawo)
{
xpos = prawo - radius;
vx *= -1;
}
else if(xpos - radius < lewo)
{
xpos = lewo + radius;
vx *= -1;
}
if(ypos + radius > dol)
{
ypos = dol - radius;
vy *= -1;
}
else if(ypos - radius < top)
{
ypos = top + radius;
vy *= -1;
}
if(zpos + radius > przod)
{
zpos = przod - radius;
vz *= -1;
}
else if(zpos - radius < tyl)
{
zpos = tyl + radius;
vz *= -1;
}
if(zpos > -fl)
{
var scale:Number = fl / (fl + zpos);
pilka.scaleX = pilka.scaleY = scale;
pilka.x = vpX + xpos * scale;
pilka.y = vpY + ypos * scale;
pilka.visible = true;
}
else
{
pilka.visible = false;
}
}
}
}
//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;
public class Odbicie3D extends Sprite
{
private var pilka:Pilka;
private var xpos:Number = 0;
private var ypos:Number = 0;
private var zpos:Number = 0;
private var vx:Number = Math.random() * 10 - 5;
private var vy:Number = Math.random() * 10 - 5;
private var vz:Number = Math.random() * 10 - 5;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
private var top:Number = -100;
private var dol:Number = 100;
private var lewo:Number = -100;
private var prawo:Number = 100;
private var przod:Number = 100;
private var tyl:Number = -100;
public function Odbicie3D()
{
init();
}
private function init():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
pilka = new Pilka(15);
addChild(pilka);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
xpos += vx;
ypos += vy;
zpos += vz;
var radius:Number = pilka.radius;
if(xpos + radius > prawo)
{
xpos = prawo - radius;
vx *= -1;
}
else if(xpos - radius < lewo)
{
xpos = lewo + radius;
vx *= -1;
}
if(ypos + radius > dol)
{
ypos = dol - radius;
vy *= -1;
}
else if(ypos - radius < top)
{
ypos = top + radius;
vy *= -1;
}
if(zpos + radius > przod)
{
zpos = przod - radius;
vz *= -1;
}
else if(zpos - radius < tyl)
{
zpos = tyl + radius;
vz *= -1;
}
if(zpos > -fl)
{
var scale:Number = fl / (fl + zpos);
pilka.scaleX = pilka.scaleY = scale;
pilka.x = vpX + xpos * scale;
pilka.y = vpY + ypos * scale;
pilka.visible = true;
}
else
{
pilka.visible = false;
}
}
}
}
//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