2010-03-31 46 views

回答

0

這聽起來像你需要知道繪圖API如何在Flash中工作。你的問題含糊不清,但這可能有助於你開始 - 谷歌「Flash繪製API」的更多信息。

var point1:Point = new Point (0, 0); 
var point2:Point = new Point (25, -50); 
var point3:Point = new Point (50, 0); 

var s:Sprite = new Sprite(); 
s.graphics.lineStyle(1, 0xff0000); 
s.graphics.beginFill(0x000000); 

s.graphics.moveTo(point1.x, point1.y); 
s.graphics.lineTo(point2.x, point2.y); 
s.graphics.lineTo(point3.x, point3.y); 
s.graphics.lineTo(point1.x, point1.y); 

s.graphics.endFill(); 
s.graphics.lineStyle(0); 

s.x = (stage.stageWidth - s.width)/2; 
s.y = ((stage.stageHeight - s.height)/2) + 50; 

addChild(s); 

我希望有幫助,讓我知道如果您有任何問題。

注意:此解決方案使用的是ActionScript 3.如果您需要使用AS2,這將無法正常工作,而且我不知道如何提供幫助,但您可能只是嘗試使用Google搜索「AS2繪圖API」或有這樣的結果。

+0

從標籤可以清楚地看到AS2是必需的 – 2010-04-01 19:07:14

1

要繪製使用ACTIONSCRIPT2形狀,您可以使用的moveTo()了lineTo()MovieClip對象的方法。您可以使用lineStyle()或使用beginFill()endFill()指定線條顏色和厚度。

所以請你圖和三角形,你可以做以下步驟:

  1. 製作一個影片剪輯名爲「圖形」
  2. 定義你的圖形應該多大(使用flash.geom.Rectangle對象)
  3. 畫出使用graph.beginFill(灰色)和然後的moveTo()和了lineTo()
  4. 畫出定期一些藍色線網格
  5. 畫出X和Y上的側軸灰色背景和底部o ˚F網格
  6. 使稱爲第二動畫片段 「形」
  7. 選擇3個隨機點:的moveTo(點1),了lineTo(點2),了lineTo(POINT3)了lineTo(點1)

這裏是如何的代碼可能是:

import flash.geom.Point; 
import flash.geom.Rectangle; 

function drawGraph(mc:MovieClip, rect:Rectangle):Void { 
//this is a function to draw the graph 

    //draw the background 
    mc.beginFill(0xF8F8F8); 
    mc.moveTo(rect.left, rect.bottom); 
    mc.lineTo(rect.left, rect.top); 
    mc.lineTo(rect.right, rect.top); 
    mc.lineTo(rect.right, rect.bottom); 
    mc.lineTo(rect.left, rect.bottom); 
    mc.endFill(); 

    //draw a grid 
    var unit:Number = 20; 
    mc.lineStyle(1, 0x0000FF, 20, true, "none", "round", "round"); 
    var i:Number=rect.x; 
    do { 
     i=i+unit; 
     mc.moveTo(i, rect.bottom); 
     mc.lineTo(i, rect.top); 
    } while (i<rect.right); 
    i=rect.bottom; 
    do { 
     i=i-unit; 
     mc.moveTo(rect.left, i); 
     mc.lineTo(rect.right,i); 
    } while (i>rect.top); 

    //draw the axes 
    mc.lineStyle(2, 0x808080, 100, true, "none", "round", "round"); 
    mc.moveTo(rect.left, rect.bottom); 
    mc.lineTo(rect.left, rect.top); 
    mc.moveTo(rect.left, rect.bottom); 
    mc.lineTo(rect.right, rect.bottom); 
} 

function randomPoint(rect:Rectangle):Point { 
//this is a function which returns a random point within rect 
    var p:Point = new Point(rect.x+Math.random()*rect.width, rect.y+Math.random()*rect.height); 
    return p; 
} 

function drawTriangle(mc:MovieClip, rect:Rectangle):Void { 
//this is a function to draw the triangle 

    // pick 3 random points within rect 
    var p1:Point = randomPoint(rect); 
    var p2:Point = randomPoint(rect); 
    var p3:Point = randomPoint(rect); 

    //connect the points to make a triangle 
    mc.lineStyle(3, 0xFF0000, 100, true, "none", "round", "round"); 
    mc.moveTo(p1.x, p1.y); 
    mc.lineTo(p2.x, p2.y); 
    mc.lineTo(p3.x, p3.y); 
    mc.lineTo(p1.x, p1.y); 

} 

//make the 'graph' clip: 
var myGraph:MovieClip = this.createEmptyMovieClip("myGraph", this.getNextHighestDepth()); 
//define the graph size: 
var myRect:Rectangle = new Rectangle(50,50,300,300); 
drawGraph(myGraph,myRect);//draw the graph 
var myShape:MovieClip = this.createEmptyMovieClip("myShape", this.getNextHighestDepth());//make the 'shape' clip 
drawTriangle(myShape,myRect);//draw a random triangle 

//add a function to draw a new triangle when the graph is clicked: 
myGraph.onRelease = function() { 
myShape.clear();//erase the old triangle 
drawTriangle(myShape,myRect);//draw a new one 
} 

可以單擊圖形生成一個新的隨機三角形。

graph + triangle http://roi.webfactional.com/img/so/graph.jpg