トップへ戻るニュースフォーラムFLASH-ML 過去ログBak@Flaダウンロードよくある質問と答
ログイン
ユーザ名:

パスワード:


パスワード紛失

新規登録
メインメニュー
メイン
   ActionScript 3.0 特設会議室
     ドラッグドロップしているオブジェクトのターゲット維持について
投稿するにはまず登録を

スレッド表示 | 新しいものから 前のトピック | 次のトピック | 下へ
投稿者 スレッド
suizenji
Åê¹ÆNo.48341
投稿日時: 2011-12-1 16:19
新米
居住地:
投稿: 2
使用環境:
win7 32bit flash cs5.5
ドラッグドロップしているオブジェクトのターゲット維持について
as3で複数のインスタンスを4*5列に並べて作成し、それぞれドラッグドロップできるようにしています。
ドロップが終わると位置が5*4の枠に収まるようにしています。

ここで問題なのですが、たまにドラッグしている最中に他のオブジェクトにターゲットが移って、配置の調整ができなくなります。
ターゲットのオブジェクトを調べると重なっているオブジェクトに変わってます。
どのようにすると最初にドラッグしたオブジェクトをドロップするまで維持できるのでしょうか。
初心者なので他の人のソースをいじりながら実験しているレベルです。よろしくおねがいします。

長くなりますが、テスト用のソースアップします。
package
{
import flash.display.Sprite;
import flash.events.Event;

public class Main extends Sprite
{
public function Main()
{

graphics.beginFill(0x000000);
graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
graphics.endFill();

var panel:Panel = new Panel();
addChild(panel);


}

}
}

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.*;
import flash.events.Event;

class Panel extends Sprite
{
public static const WIDTH:int = 5;
public static const HEIGHT:int = 4;
public static const cWIDTH:int = 5;
public static const cHEIGHT:int = 4;
public static var Count:int=0;

private var blocks:Array;
private var card_Arr:Array;

public function Panel()
{
createBlocks();
}

private function createBlocks():void
{
blocks = new Array(WIDTH);

for (var y:int = 0; y < HEIGHT; y++)
{
blocks[y] = new Array(HEIGHT);

for (var x:int = 0; x < WIDTH; x++)
{
var block:Block = new Block();
block.x = x * Block.WIDTH;
block.y = y * Block.HEIGHT;
addChild(block);

blocks[y][x] = block;
Count++;
}
}

card_Arr = new Array(cWIDTH);

for (y = 0; y < cHEIGHT; y++)
{
card_Arr[y] = new Array(cHEIGHT);

for (x = 0; x < cWIDTH; x++)
{
card_Arr[y][x] =0;
}
}

trace(card_Arr);
}
}

class Block extends Sprite
{
public static const WIDTH:int = 32*2; // ブロックの横幅
public static const HEIGHT:int = 48*2; // ブロックの縦幅
public static const MARGIN_W:int = 1;
public static const MARGIN_H:int = 1;
public var count:int;

public function Block()
{
graphics.beginFill(0xFFFFFF);
graphics.drawRect(MARGIN_W, MARGIN_H, WIDTH - MARGIN_W * 2, HEIGHT - MARGIN_H * 2);
graphics.endFill();
count=Panel.Count;


addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);

addEventListener(MouseEvent.MOUSE_UP, onMouseUp);


}


private function onMouseDown(event:MouseEvent):void
{
startDrag(false, new Rectangle(0, 0, stage.stageWidth-64, stage.stageHeight-96));
stage.addEventListener(Event.MOUSE_LEAVE, onMouseLeave);
trace("begin count="+count);
}


private function onMouseUp(event:MouseEvent):void
{
stopDrag();


var l_pos : Point = new Point(0,0);
var g_pos : Point = this.localToGlobal(l_pos);
this.x=int(g_pos.x/(64))*64;
this.y=int(g_pos.y/(96))*96;
trace("x="+this.x+"/ y="+this.y);
stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeave);
trace("end count="+count);
}


private function onMouseLeave(event:Event):void
{
stopDrag();
stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeave);

var l_pos : Point = new Point(0,0);
var g_pos : Point = this.localToGlobal(l_pos);
this.x=int(g_pos.x/(64))*64;
this.y=int(g_pos.y/(96))*96;
trace("x="+this.x+"/ y="+this.y);
stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeave);
trace("end count="+count);
}

}
vader
Åê¹ÆNo.48342
投稿日時: 2011-12-1 19:06
常連
居住地: 東京
投稿: 76
使用環境:
MacOS10.8,FlashCS6
Re: ドラッグドロップしているオブジェクトのターゲット維持について
suizenjiさん、こんにちわ。

問題は、重なり順が下のインスタンスのドラッグを重なり順が上のインスタンスの上で終了した際に起こっているようです。
つまり、ドラッグしているインスタンスとは別のインスタンスでonMouseUpが実行されているわけです。

解決策として1番簡単だと思うのは、mouseDownしたインスタンスの重なり順を1番上にしておくことです。


package {
	import flash.display.Sprite;
	import flash.events.Event;

	public class Main extends Sprite {
		public function Main() {

			graphics.beginFill(0x000000);
			graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			graphics.endFill();

			var panel:Panel = new Panel();
			addChild(panel);
		}
	}
}

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.*;
import flash.events.Event;

class Panel extends Sprite {
	public static const WIDTH:int = 5;
	public static const HEIGHT:int = 4;
	public static const cWIDTH:int = 5;
	public static const cHEIGHT:int = 4;
	public static var Count:int = 0;
	
	private var blocks:Array;
	private var card_Arr:Array;
	
	public function Panel() {
		createBlocks();
	}
	
	private function createBlocks():void {
		blocks = new Array(WIDTH);
		for (var y:int = 0; y < HEIGHT; y++) {
			blocks[y] = new Array(HEIGHT);
			for (var x:int = 0; x < WIDTH; x++) {
				var block:Block = new Block();
				block.x = x * Block.WIDTH;
				block.y = y * Block.HEIGHT;
				addChild(block);
				blocks[y][x] = block;
				Count++;
			}
		}
		
		card_Arr = new Array(cWIDTH);
		
		for (y = 0; y < cHEIGHT; y++) {
			card_Arr[y] = new Array(cHEIGHT);
			for (x = 0; x < cWIDTH; x++) {
				card_Arr[y][x] = 0;
			}
		}
		trace(card_Arr);
	}
}

class Block extends Sprite {
	public static const WIDTH:int = 64; // ブロックの横幅
	public static const HEIGHT:int = 96; // ブロックの縦幅
	public static const MARGIN_W:int = 1;
	public static const MARGIN_H:int = 1;
	public var count:int;
	
	public function Block() {
		graphics.beginFill(0xFFFFFF);
		graphics.drawRect(MARGIN_W, MARGIN_H, WIDTH - MARGIN_W * 2, HEIGHT - MARGIN_H * 2);
		graphics.endFill();
		count = Panel.Count;
		
		addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
	}
	
	private function onMouseDown(event:MouseEvent):void {
		var target:Block = Block(event.target);
		var panel:Panel = Panel(target.parent);
		panel.setChildIndex(target, panel.numChildren - 1);
		
		startDrag(false, new Rectangle(0, 0, stage.stageWidth - 64, stage.stageHeight - 96));
		stage.addEventListener(Event.MOUSE_LEAVE, onMouseLeave);
		trace("begin count=" + count);
	}
	
	private function onMouseUp(event:MouseEvent):void {
		stopDrag();
		
		var l_pos:Point = new Point(0, 0);
		var g_pos:Point = this.localToGlobal(l_pos);
		
		this.x = int(g_pos.x / 64) * 64;
		this.y = int(g_pos.y / 96) * 96;
		trace("x=" + this.x + "/ y=" + this.y);
		stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeave);
		trace("end count=" + count);
	}
	
	private function onMouseLeave(event:Event):void {
		stopDrag();
		stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeave);
		
		var l_pos:Point = new Point(0, 0);
		var g_pos:Point = this.localToGlobal(l_pos);
		this.x = int(g_pos.x / 64) * 64;
		this.y = int(g_pos.y / 96) * 96;
		trace("x=" + this.x + "/ y=" + this.y);
		//stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeave);
		trace("end count=" + count);
	}
}


以上、頑張って下さい。
suizenji
Åê¹ÆNo.48343
投稿日時: 2011-12-1 22:38
新米
居住地:
投稿: 2
使用環境:
win7 32bit flash cs5.5
Re: ドラッグドロップしているオブジェクトのターゲット維持について
こんにちは vader様。
重なり順など全く考えてもおりませんでした。

お早い回答と解決策とても感謝しております。

スレッド表示 | 新しいものから 前のトピック | 次のトピック | トップ

投稿するにはまず登録を
 
Copyright (C) 2003 FLASH-japan. All rights reserved.
Powered by Xoops