2011年6月26日 星期日

Flash Actionscript Physics Engine(APE) II

image

image

image

其中會選用這個工具是因為在其中一個Demo中,右邊這個被命名為SwingDoor的小組件,應該能等合,反抗地心引力(Gravity)的感受

Decompile 後SwingDoor的程式碼如下:

  1: //class SwingDoor
  2: package compcar
  3: {
  4:     import org.cove.ape.*;
  5: 
  6:     public class SwingDoor extends org.cove.ape.Group
  7:     {
  8:         public function SwingDoor(arg1:uint)
  9:         {
 10:             var loc1:*=null;
 11:             var loc2:*=null;
 12:             var loc3:*=null;
 13:             var loc4:*=null;
 14:             var loc5:*=null;
 15:             var loc6:*=null;
 16:             var loc7:*=null;
 17:             super();
 18:             collideInternal=true;
 19:             loc1=new org.cove.ape.CircleParticle(543, 55, 7);
 20:             loc1.mass=0.001;
 21:             loc1.setStyle(1, arg1, 1, arg1);
 22:             addParticle(loc1);
 23:             loc2=new org.cove.ape.CircleParticle(620, 55, 7, true);
 24:             loc2.setStyle(1, arg1, 1, arg1);
 25:             addParticle(loc2);
 26:             (loc3=new org.cove.ape.SpringConstraint(loc1, loc2, 1, true, 13)).setStyle(2, arg1, 1, arg1);
 27:             addConstraint(loc3);
 28: 
 29: 
 30:             (loc4=new org.cove.ape.CircleParticle(543, 5, 2, true)).visible=true;
 31:             loc4.collidable=false;
 32:             addParticle(loc4);
 33: 			
 34:             (loc5=new org.cove.ape.SpringConstraint(loc1, loc4, 0.02)).restLength=40;
 35:             loc5.visible=true;
 36:             addConstraint(loc5);
 37: 			
 38:             (loc6=new org.cove.ape.CircleParticle(550, -60, 70, true)).visible=true;
 39:             addParticle(loc6);
 40: 			
 41:             (loc7=new org.cove.ape.RectangleParticle(650, 130, 42, 70, 0, true)).visible=false;
 42:             addParticle(loc7);
 43:             return;
 44:         }
 45:     }
 46: }
 47: 
 48: 
 49: 


line 19-22 :loc1 : 棒子左邊的點(point),為不固定(fixed = false)



line 23~25: loc2:棒子右邊的點(point),為固定(fixed = true)



line 26~27:loc3: 棒子本身,利用loc1,及loc2串起棒子



棒子可因重物壓過即造成往下掉,和放開時,又往上彈,即時利用了以下二個元素



line 30~32:loc4:在loc1的正上方放即一個點為固定(fixed=true),並設不可碰撞(collidable=false)



line 34~36:loc5:把loc1及loc4用一條線串起來



其中loc4及loc5不希望使用者看到把是否可視設為不可見(visible=false)



line 38~39: 因棒子擺回的動作太大時會造成無法回到原定位,加了一個大球把棒子擋住





image    



image



把loc4~loc7設為可見後,即可清楚其運作



接下應該有機會應用這個範例,加上一些自己的想法,運用在實際專案下,Good Luck






Car Demo Source Code



2011年6月24日 星期五

Flash Actionscript Physics Engine(APE) I

這個物理引擎算時是有點老

不過基於簡單原則,這幾天開始研究起這個工具,不過由於官網上的說明真的太少了

雖然說是簡單,但也花了二天找了一下相關的資源

首先當然就是官網  http://www.cove.org/ape/

資料真是少的可憐  只有二個Demo , As Doc ,及一個簡單到不可再簡單的Tutorial

這個引擎確實是簡單的多,主要的組成

  1. 把引擎初始化
  2. 設定在畫面上的物件
  3. 設定群組並加入引擎
  4. 讓引擎不斷重繪

真的就是這幾個步驟

另外這個引擎主要的元素也不多

  1. CircleParticles
  2. RectangleParticles
  3. WheelParticles
  4. SpringConstraints   -  稍微複雜的物件即可用這個元素拼出來
  5. Grouping    - 更複雜的物件就用到這個元素
  6. Collision

即然只有二個Demo,其實其中二個demo的內容還蠻值得參考的,不過因為未提供source

只好把它decompile一下,了解過內容對於基本的物理運動要求應該是可以達到的  ^_^

以下是幾個網上找到的資源,其中需要在該這些網多翻一些其它頁面,可以更多範例

2011年6月2日 星期四

stateGroups 簡易範例

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955"
minHeight="600">

<fx:Script>
<![CDATA[
protected function button1_clickHandler(event:MouseEvent):void
{
this.currentState='one';
}

protected function button2_clickHandler(event:MouseEvent):void
{
this.currentState='two';
}

protected function button3_clickHandler(event:MouseEvent):void
{
this.currentState='three';
}
]]>
</fx:Script>

<s:states>
<s:State name="one"
stateGroups="o"/>
<s:State name="two"
stateGroups="t"/>
<s:State name="three"
stateGroups="o,t"/>
</s:states>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button x="32"
y="346"
label="Button"
click="button1_clickHandler(event)"/>
<s:Button x="106"
y="346"
label="Button"
click="button2_clickHandler(event)"/>
<s:Button x="184"
y="346"
label="Button"
click="button3_clickHandler(event)"/>
<s:BorderContainer includeIn="o"
x="32"
y="32"
width="200"
height="200"
backgroundColor="#F01010">
</s:BorderContainer>
<s:BorderContainer includeIn="t"
x="263"
y="32"
width="200"
height="200"
backgroundColor="#420BF5">
</s:BorderContainer>
</s:Application>