-
Mouse Detection and Speed detection Handlers
Hey, I'm new to flash and need help badly!
Basically i need to know how to work out the direction of the mouse on stage and say from this:
"if the mouse goes from the left to right (visa-versa) once, add score +1"
and
"if the mouse speed which is player controlled - slows down - to then display an error graphic"
your fast responses and help on this matter will be greatly appreciated.
Thanks in advance,
Taran
Last edited by taransingh; 04-24-2011 at 10:05 AM.
-
You can use the MouseEvent.MOUSE_MOVE event to detect when the mouse has changed position. To get it's speed and direction, keep track of the last position and the current position (the MouseEvent passed in to your event handler will have localX, localY, as well as stageX, and stageY properties). You can subtract the previous values from the current values to get the difference, which will tell you the direction and distance between them. The distance is the instantaneous speed, between the two mouse move events. You may want to keep the time the events occurred as well to do more sophisticated speed calculations.
-
Fantastic, thank you for your quick reply, I will give this a go and get back to you.
-
Hi, apologies but I'm having some trouble coding this as everything I have tried brings errors. And I think I'm doing it completely wrong anyway so could you give me a nudge in the right direction please with a snippet of code for this?
Thanks,
-
Sure. I'll just put some debug info in TextFields on the display.
Code:
//initialize these to sentinel value so we can tell later if they are legit
var lastX:Number = NaN;
var lasyY:Number = NaN;
var speed:Number;
var speedText:TextField = new TextField();
addChild(speedText);
var xDiffText:TextField = new TextField();
xDiffText.y = 20;
addChild(xDiffText);
var yDiffText:TextField = new TextField();
yDiffText.y = 40;
addChild(yDiffText);
addEventListener(Event.ENTER_FRAME, calculateMouseSpeed);
function calculateMouseSpeed(e:Event):void{
if (isNaN(lastX) || isNan(lastY)){
lastX = stage.mouseX;
lastY = stage.mouseY;
return;
}
var xDiff:Number = stage.mouseX - lastX;
var yDiff:Number = stage.mouseY - lastY;
speed = Math.sqrt(xDiff*xDiff + yDiff*yDiff);
speedText.text = speed;
xDiffText.text = xDiff;
yDiffText.text = yDiff;
}
I decided to go with ENTER_FRAME rather than MOUSE_MOVE because it occurs regularly and speed is easy to determine.
-
Thank you, I knew I was on the right track. Just had it in the wrong order. The last 2 lines of code there bring the error 'implicit coercion of a value of type Number to an unrelated type String.' ?? I'm guessing this is related to the conversion but again I know how to convert Int - String in Java but not actionscript. Guidance please..
Last edited by taransingh; 04-24-2011 at 09:20 AM.
-
Only the last 2? I would expect it on the last 3 if it happens at all.
I didn't test the code, some tweaks are to be expected.
Also I forgot to set lastX and lastY in the normal case.
Try using the global String function to convert the Number to a String:
Code:
speedText.text = String(speed);
xDiffText.text = String(xDiff);
yDiffText.text = String(yDiff);
lastX = stage.mouseX;
lastY = stage.mouseY;
-
Fantastic, that works. How would I say with this code, 'if speed decreases - trace to output "slowing down"' and 'if user goes left to right once - add to score'? I know in pseudo it's 'if )xdiff || ydiff < lastXDiff || last Ydiff) { perform action' but not sure of the lines of code.. any suggestions for this?
Last edited by taransingh; 04-24-2011 at 09:22 AM.
-
You can obviously keep the last speed just like you keep the lastX coordinate. Just define a variable and put the right values in it at the right time. You can then compare speed to lastSpeed and determine if the mouse is slowing down.
To determine if the mouse is going left to right, that just means that xDiff is positive.
-
Thanks, I'll give this a try now and get back to you.
-
Okay, so I've got:
var lastSpeed:Number = NaN;
lastSpeed = Math.sqrt(lastX*lastX + lastY*lastY);
if (lastSpeed >= speed) {
trace("slower");
}
but somehow I don't think this is quite right?
-
No, it's not right because you cannot directly compare anything to NaN. But you do have an initial speed whereas you did not have an initial location.
Just set lastSpeed to 0 when you start, instead of NaN. Remove the line which attempts to set lastSpeed based on lastX and lastY, that makes no sense.
-
Originally Posted by 5TonsOfFlax
To determine if the mouse is going left to right, that just means that xDiff is positive.
I think there's a misunderstanding. How would I approach saying WHEN the mouse goes left AND right once AND the current speed is more than the previous speed DO THIS?
Apologies for lack of clarity.
Your responses have been very helpful with the suggestions made.
Thanks again,
-
The mouse cannot go left and right at the same time. What do you actually want? If you just want to make sure it has made at least one leftward move and one rightward move, then keep booleans for those, and use xDiff to set them appropriately in the enter frame. Your if statement might look like this:
Code:
if (hasGoneLeft && hasGoneRight && (speed > lastSpeed)){
//do stuff
}
That seems like a really weird thing to check for, though.
-
Perfect, yes to detect when the mouse has made one leftward move and then one rightward move is what I need but don't completely know the correct syntax for it. I've tried this check in many ways and they all seem to fail, this seems the most fitting, one of which I hadn't tried due to lack of syntax terminology and understanding. I've hunted around for further suggestions on this piece of code to figure it out on my own fully however, like how you've mentioned it is a different piece of checking, I cannot find it anywhere. Would you be able to assist me with the matter?
Last edited by taransingh; 04-25-2011 at 04:38 PM.
-
^^ No worries just yet. I'll try a couple more with different code, I think I may have figured out with your help..
Last edited by taransingh; 04-25-2011 at 04:53 PM.
-
I know that if I was to do the previous IF mouse goes left or right it would look something like:
}
Actionscript Code:
var xDiff = event.stageX - this.oldMouseStageX; if (xDiff > 0) { //do stuff
but I'm not too sure on how to define the left and right for this current statement of if the mouse moves leftward && if the mouse moves rightward.
-
Like I said before, you need to store the fact that there has been a leftward movement in a variable and also store the fact that there has been a rightward movement in a variable. And since you care about the order, that complicates it a little. The following code needs to be in your initialization:
Code:
var hasGoneLeft:Boolean = false;
var hasGoneRight:Boolean = false;
And this needs to be in your enter frame handler
Code:
//set if mouse has gone left
if (xDiff < 0){
hasGoneLeft = true;
}
//if we already detected a left, and now detect a right, set that.
if (hasGoneLeft && xDiff > 0){
hasGoneRight = true;
}
if (hasGoneLeft && hasGoneRight && (speed > lastSpeed)){
//do something.
}
-
Originally Posted by 5TonsOfFlax
Like I said before, you need to store the fact that there has been a leftward movement in a variable and also store the fact that there has been a rightward movement in a variable. And since you care about the order, that complicates it a little. The following code needs to be in your initialization:
Code:
var hasGoneLeft:Boolean = false;
var hasGoneRight:Boolean = false;
And this needs to be in your enter frame handler
Code:
//set if mouse has gone left
if (xDiff < 0){
hasGoneLeft = true;
}
//if we already detected a left, and now detect a right, set that.
if (hasGoneLeft && xDiff > 0){
hasGoneRight = true;
}
if (hasGoneLeft && hasGoneRight && (speed > lastSpeed)){
//do something.
}
hmmm.. I did this exactly but it did not work.. So I assumed I was doing it wrong. Okay, thank you, I will give this a try and again, have a thorough look for the differences and return a reply later on.
-
Put traces in, and find out what it's doing or not doing. I'm not (quite) infallible, and might have missed something.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|