1

I am making a game which uses collision detection with the mouse.

The player is a custom mouse cursor when the mouse collies with an object the mouse is moved to the coordinates X0,Y0. The code I have used to achieve this is below. However when the mouse is moved to X0,Y0 after a collision when the mouse is moved it starts back where the collision took place rather than moving from the top of the screen.

import flash.events.Event;

var cursor:MovieClip;

function initializeMovie ():void {

cursor = new Cursor();
addChild (cursor);
cursor.enabled = false;
Mouse.hide ();
stage.addEventListener (MouseEvent.MOUSE_MOVE, dragCursor);
}

function dragCursor (event:MouseEvent):void{

cursor.x = this.mouseX;
cursor.y = this.mouseY;
}

initializeMovie ();

this.addEventListener( Event.ENTER_FRAME, handleCollision)

function handleCollision( e:Event ):void{

if(cursor.hitTestObject( wall )){
cursor.x = 0
cursor.y = 0
     }

}
user1078385
  • 344
  • 2
  • 6
  • 21

2 Answers2

1

When you reset the position of cursor object you aren't moving the actual mouse position, I don't believe you can actual do what you're trying to (that is write to the mouse position to move the users cursor, I believe this would require system specific code, like C# or I believe Objective C and Cocoa or Carbon on the Mac side).

http://www.kirupa.com/forum/showthread.php?354641-Possible-to-set-mouse-position-in-flash

Here's a way you would do it on Mac apparently https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/Quartz_Services_Ref/Reference/reference.html#//apple_ref/c/func/CGWarpMouseCursorPosition

And a way on Windows Set Mouseposition in WPF

And a way on Linux (Although this only has AIR support up to 2.6) How to set the mouse position under X11 (linux desktop)?

So if you implemented both of those solutions and are packaging as an AIR app you could make this work, but otherwise I'm pretty sure it's impossible.

Community
  • 1
  • 1
shaunhusain
  • 19,630
  • 4
  • 38
  • 51
  • And thus, the best solution to your problem would be to instead of moving the mouse, make user "grab" a square at some specific pixel and move this thing around, and then reset this objects position to (0,0) – Daniel MesSer Aug 08 '12 at 08:12
1

Create a button at the 0, 0 coord that is required to click to continue again. Then your user will have to move the mouse to to that spot where you can continue to have the custom cursor track your mouse.

IAmMearl
  • 487
  • 2
  • 7