Copy link to clipboard
Copied
I want to make drag and drop at certain coordinate.
I have two moving blocks, where player can drag any block at first time onto holder 1. Then, drag another block at second time onto holder 2.
When the block did not hits either holder 1 or holder 2, it will terminate and that block back to the initial coordinate.
When both hit both coordinate of holder, then move to next scene.
import flash.events.Event;
stop();//stop the scene
var startingPt:Point = new Point();//declaration variable
var movingBlock:Sprite;
var placed:int = 0;//initialize
var blok1:Boolean=false;
var blok2:Boolean=false;
var endpoint:Boolean=false;
block3.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);//function
block2.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
function startBlockMove(evt:MouseEvent):void {//the object move
movingBlock = Sprite(evt.currentTarget);
startingPt.x = movingBlock.x;
startingPt.y = movingBlock.y;
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveBlock);
}
function moveBlock(e:MouseEvent):void {//object move based on mouse coordinate
movingBlock.x = stage.mouseX;
movingBlock.y = stage.mouseY;
}
stage.addEventListener(MouseEvent.MOUSE_UP, stopMotion);//stop move
function stopMotion(evt:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveBlock);
snapInPlace();
movingBlock = new Sprite();
startingPt = new Point();
}
function snapInPlace():void {//
if(blok1==false || blok2==false){
if (holder.hitTestObject(movingBlock)) {//object1 hits the goal
movingBlock.x = holder.x;
movingBlock.y = holder.y ;
placed++;
movingBlock.removeEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
blok1=true;
}
else if (holder1.hitTestObject(movingBlock)) {//object2 hits the goal
movingBlock.x = holder1.x;
movingBlock.y = holder1.y;
movingBlock.removeEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
blok2=true;
}
else {
movingBlock.x = startingPt.x;//if not hits the goal, return to original coordinate
movingBlock.y = startingPt.y;
blok1=false;
blok2=false;
}
}
else if(blok1==true && blok2==true){
setTimeout (function (){ endpoint=true;
trace("You died!");
removeEventListener(Event.REMOVED, snapInPlace);
gotoAndStop(1,"Scene 2");}, 1000);
}
}
The output gives error,
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Untitled_2_fla::MainTimeline/snapInPlace()[Untitled_2_fla.MainTimeline::frame2:38]
at Untitled_2_fla::MainTimeline/stopMotion()[Untitled_2_fla.MainTimeline::frame2:31]
Where at frame2:38
if (holder.hitTestObject(movingBlock)) {}
and at frame2:31
snapInPlace();
How to solve this?
Use this code:
var startingPt:Point = new Point();
var placed:int = 0;
block2.buttonMode = block3.buttonMode = true;
block2.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block3.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block2.addEventListener(MouseEvent.MOUSE_UP, stopBlockMove);
block3.addEventListener(MouseEvent.MOUSE_UP, stopBlockMove);
function startBlockMove(evt:MouseEvent):void
{
startingPt.x = evt.target.x;
startingPt.y = evt.target.y;
evt.target.startDrag();
}
func
...Copy link to clipboard
Copied
Use this code:
var startingPt:Point = new Point();
var placed:int = 0;
block2.buttonMode = block3.buttonMode = true;
block2.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block3.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block2.addEventListener(MouseEvent.MOUSE_UP, stopBlockMove);
block3.addEventListener(MouseEvent.MOUSE_UP, stopBlockMove);
function startBlockMove(evt:MouseEvent):void
{
startingPt.x = evt.target.x;
startingPt.y = evt.target.y;
evt.target.startDrag();
}
function stopBlockMove(evt:MouseEvent):void
{
evt.target.stopDrag();
if(evt.target == block2 && evt.target.hitTestObject(holder1))
{
evt.target.x = holder1.x
evt.target.y = holder1.y
evt.target.removeEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
placed +=1;
}
else if(evt.target == block3 && evt.target.hitTestObject(holder2))
{
evt.target.x = holder2.x
evt.target.y = holder2.y
evt.target.removeEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
placed +=1;
}
else
{
evt.target.x = startingPt.x
evt.target.y = startingPt.y
}
////
if(placed == 2)
{
gotoAndStop(1,"Scene 2");
}
}
Copy link to clipboard
Copied
try:
safiahn40752198 wrote:
I want to make drag and drop at certain coordinate.
I have two moving blocks, where player can drag any block at first time onto holder 1. Then, drag another block at second time onto holder 2.
When the block did not hits either holder 1 or holder 2, it will terminate and that block back to the initial coordinate.
When both hit both coordinate of holder, then move to next scene.
import flash.events.Event;
stop();//stop the scene
var startingPt:Point = new Point();//declaration variable
var movingBlock:Sprite;
var placed:int = 0;//initialize
var blok1:Boolean=false;
var blok2:Boolean=false;
var endpoint:Boolean=false;
block3.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);//function
block2.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
function startBlockMove(evt:MouseEvent):void {//the object move
stage.addEventListener(MouseEvent.MOUSE_UP, stopMotion);//stop move
movingBlock = Sprite(evt.currentTarget);
startingPt.x = movingBlock.x;
startingPt.y = movingBlock.y;
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveBlock);
}
function moveBlock(e:MouseEvent):void {//object move based on mouse coordinate
movingBlock.x = stage.mouseX;
movingBlock.y = stage.mouseY;
}
// stage.addEventListener(MouseEvent.MOUSE_UP, stopMotion);//stop move
function stopMotion(evt:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, stopMotion);//stop move
stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveBlock);
snapInPlace();
movingBlock = new Sprite();
startingPt = new Point();
}
function snapInPlace():void {//
if(blok1==false || blok2==false){
if (holder.hitTestObject(movingBlock)) {//object1 hits the goal
movingBlock.x = holder.x;
movingBlock.y = holder.y ;
placed++;
movingBlock.removeEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
blok1=true;
}
else if (holder1.hitTestObject(movingBlock)) {//object2 hits the goal
movingBlock.x = holder1.x;
movingBlock.y = holder1.y;
movingBlock.removeEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
blok2=true;
}
else {
movingBlock.x = startingPt.x;//if not hits the goal, return to original coordinate
movingBlock.y = startingPt.y;
blok1=false;
blok2=false;
}
}
else if(blok1==true && blok2==true){
setTimeout (function (){ endpoint=true;
trace("You died!");
removeEventListener(Event.REMOVED, snapInPlace);
gotoAndStop(1,"Scene 2");}, 1000);
}
}
The output gives error,
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Untitled_2_fla::MainTimeline/snapInPlace()[Untitled_2_fla.MainTimeline::frame2:38]
at Untitled_2_fla::MainTimeline/stopMotion()[Untitled_2_fla.MainTimeline::frame2:31]
Where at frame2:38
if (holder.hitTestObject(movingBlock)) {}
and at frame2:31
snapInPlace();
How to solve this?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now