2012-01-07 2 views
0

mouseDown関数で円を作成するコードを作成しました。私はサークルのxとyのコードを保存しようとしていますが、これを回避する方法を見つけることはできません。私はmouseDownからコードの複数のインスタンスを保存する必要があります。マウスクリック時にxとyコードを変数として保存する

これをアレイで行うことはできますか?

P.Sコードを投稿する人はいません。ただ助言してください。 AS3を使用しており、私はかなり新しくなっています。

var posOne:Number; 
//var posTwo:Number; 
//var posThree:Number; 
//var posFour:Number; 
//var posFive:Number; 



import flash.events.MouseEvent; 





stage.addEventListener(MouseEvent.MOUSE_DOWN,startDoodle); 
stage.addEventListener(MouseEvent.MOUSE_UP,stopDoodle); 

    function startDoodle(e:MouseEvent):void{ 
    stage.addEventListener(MouseEvent.MOUSE_DOWN,makeTarget); 
} 




function stopDoodle(e:MouseEvent):void{ 

    stage.removeEventListener(MouseEvent.MOUSE_UP,makeTarget); 

} 





import flash.events.MouseEvent; 


function makeTarget(e:MouseEvent):void { 

var ellipse:Ellipse = new Ellipse (15,15, 0x00ff00); 
addChild (ellipse); 
ellipse.x = mouseX; 
ellipse.y = mouseY; 

//posOne == ellipse.x && ellipse.y 


} 
+0

これまでのコード設定の方法によって異なりますが、変数に似ている文字列を生成して実行時に連結するのが簡単な方法です。トレース/出力から「コード」をコピーするか、[setClipboard()](http://help.adobe.com/jp/FlashPlatform/reference/actionscript/3/flash/system/System)を使用してください。 html#setClipboard())メソッドを使用して文字列をコピーします。 –

+0

これまでのコードを追加しました。返信ありがとう:D – dMo

+0

posOne/Two/etc。 Number変数なので、1つの位置コンポーネント(xまたはyのいずれか)を格納したいと思いますが、同時に両方を格納しないとしますか?また、5つの値を保存したいので、5つの最新のマウス位置が必要ですか?その場合は、配列に新しい値を追加する際に、値を格納する配列を持っていて、古い値を1つシフトする必要があります。 –

答えて

1

これにアプローチする最も簡単な方法は、マウスのクリックのxy座標でPointオブジェクトを作成し、配列にそれらをプッシュするだろう。

function onMouseClick(event:MouseEvent):void 
{ 
    var mousePoint:Point = new Point(stage.mouseX, stage.mouseY); 
    mouseClicks.push(mousePoint); 
} 

あなたはcoordsのこれらのタイプの負荷を格納する必要がある、とあなたは、パフォーマンスやメモリを心配している場合、あなたがいることを、85|56$104|77$...のように、特定の区切り文字を含む、特定の形式で、文字列と文字列表現を座標救うことができますxyの各セットは1つのシンボルで区切られ、セット内のxyの値は他の区切り文字で区切られています。このデータメモリを賢明に保存するには、入力を16ビット整数に制限し、両方の値を32ビット整数(最初の16ビットにはxの値、最後の16ビットにはyの値)に格納します。オペレーション。

+0

おそらく、値を保存して文字列に連結してしまうと、実際にはint値やNumber値より多くのメモリを消費します。 'int'値を配列にプッシュし、' array.shift'でそれらの2つを取り出し、最初を 'x'、2番目を' y'として扱います。 – joncys

0

私は実際には説明するよりもコードで見やすいと思います。マウスポイントの使用については読んではいけませんが、概要については、上記のコードを参考にして、ポイントの使用を開始することをお勧めします。詳細はコメントをご覧ください。

// It helps to keep all the imports at hte top of your file, for easy code reading. 

import flash.events.MouseEvent; 
import flash.geom.Point; 

// It helps to keep all class-level variables at the top of the file. 

// Make an array rather than many individual variables. That way you can use a for-loop to access the items. 
// (The brackets "[]" are equivalent to new Array().) 
var localPosArray:Array = []; 
var globalPosArray:Array = []; 
var ellipseArr:Array = []; 

// can use these as temporary variables: 
var localPoint:Point; 
var globalPoint:Point; 

// stores currently active ellipse index: 
var curEllipseIdx:int; 

// stores currently active point index: 
var curPtIdx:int; 

// store click count: 
// (You could use an array instead, but probably not necessary.) 
var countTotal:int = 0; 

// Probably not needed: 
// var posOne:Number; 
//var posTwo:Number; 
//var posThree:Number; 
//var posFour:Number; 
//var posFive:Number; 

stage.addEventListener(MouseEvent.MOUSE_DOWN, startDoodle); 
stage.addEventListener(MouseEvent.MOUSE_MOVE, sizeDoodle); 
stage.addEventListener(MouseEvent.MOUSE_UP, stopDoodle); 

function startDoodle(ev:MouseEvent):void 
{ 
    // count clicks (mouse downs): 
    countTotal++; 

    // to find the coordinates on the local object -- this is usually different from the global coordinates. 
    curPtIdx = localPosArray.length; 
    localPosArray[curPtIdx] = new Point(ev.localX, ev.localY); 

    // alternately, to overwrite a current point: 
    // curPtIdx = 0; 
    // localPosArray[curPtIdx] = new Point(ev.localX, ev.localY); // the old value will be garbage collected, if there are no other references. 

    // to convert from local to global coordinates: 
    // curPtIdx = 0; 
    // globalPoint = localToGlobal(localPosArray[curPtIdx]); 


    // alternately, to find the coordinates on the global object (the stage): 
    curPtIdx = globalPosArray.length; 
    globalPosArray[globalPosArray.length] = new Point(ev.stageX, ev.stageY); 

    // alternately, to overwrite a current point: 
    // curPtIdx = 0; 
    // globalPosArray[curPtIdx] = new Point(ev.stageX, ev.stageY); // the old value will be garbage collected, if there are no other references. 

    // to convert from local to global coordinates: 
    // curPtIdx = 0; 
    // localPoint = globalToLocal(globalPosArray[curPtIdx]); 


    //You do not need to stop listening for mouse *down*, since that can never happen when the mouse is down. 
    // stage.addEventListener(MouseEvent.MOUSE_DOWN, makeTarget); 
} 

function stopDoodle(e:MouseEvent):void 
{ 
    //You do not need to stop listening for mouse *up*, since that can never happen when the mouse is up. 
    //stage.removeEventListener(MouseEvent.MOUSE_UP, makeTarget); 
} 

function makeTarget(e:MouseEvent):void 
{ 
    curPtIdx = 0; 
    curEllipseIdx = ellipseArr.length; 
    ellipseArr[curEllipseIdx] = new Ellipse(localPosArray[curPtIdx].x, localPosArray[curPtIdx].x, 0x00ff00); 
    addChild(ellipseArr[curEllipseIdx]); 

    // These lines are probably not necessary: 
    //ellipse.x = mouseX; 
    //ellipse.y = mouseY; 

    // What is this for? 
    //posOne == ellipse.x && ellipse.y 
} 

function sizeDoodle(ev:MouseEvent):void 
{ 
    if (ellipseArr && ellipseArr[curEllipseIdx]) 
    { 
     // size the ellipse by the distance from the initial point: 
     // however you might do that. 
    } 
} 
関連する問題