Why is the trace not happening in my MouseEvent.CLICK handler? - actionscript-3

Okay so I'm making a really simple game where you have to click and move the mouse back and forth across a certain area to gain points.
I have a hit MovieClip, but it won't trace hovers or even clicks when I certainly am clicking there. Is it the code or is it my fault?
If you could provide any insight into how to properly have a counter that increases when you have a MouseEvent.MOUSE_DOWN and are repeatedly moving the mouse over the test area that would be so good.
var counter:int = 0
hit.addEventListener(MouseEvent.CLICK, fl_MouseOverHandler);
function fl_MouseOverHandler(event:MouseEvent):void
{
counter++;
trace("Moused over");
}

Related

How to implement click event and drag event in HTML Canvas

I have created a business tool using html canvas.
It contains numerous elements that can be dragged and dropped. I have coded this functionality myself basically saying (pseudocode):
if(mouseIsDown && mouseInBoundsOfIcon){
icon.grabbed = true;
}
if(icon.grabbed){
icon.x = mouseX;
icon.y = mouseY;
}
Now I would like some of these icons to implement an onClick event as well, making them both clickable and 'drag & droppable'.
The problem is the mousedown event gets fired as soon as I click down on a mouse button (obviously). I'm thinking I could use a short timer, when mousedown fires and mouseInBoundsOfIcon==true, I can start it, when it hits 0.5 seconds for example icon.grabbed = true, else if the button is released it counts as a click?
I'm not sure how to go about this. Any suggestions?
There are at least 2 common ways of differentiating between clicks and drag-starts.
Both these methods involve taking a measurement during mousedown and comparing it to another measurement taken in either mouseup or mousemove.
Measure the distance the mouse moves. For example, if it moves more than 5 pixels before mouseup it's a drag.
Measure the time between mousedown and mouseup. For example, if mouseup occurs within 200ms of mousedown, then it's a click.
Helpful Hint: Let mousedown default to drag-start rather than click. Start dragging on mousedown. If you later determine the user intended a click then put the icon back at its starting position so they don't have slightly nudged icons when intending clicks.

My AS3 code will not run my animation properly

I want to be able to press any key on my keyboard so as to appear to be typing my specific
text. No matter what key is pressed, the same scripted text appears, letter by letter(one per keystroke)/frame by frame. Can anyone help me here? As it stands, my animation just continues to loop unless I hit enter (then it stops until pressed again.)
stop();
var keyList:Object = new Object();
keyList.onKeyUp = function () {
nextFrame();
}
Key.addListener(keyList);
stop();
Using Flash cc-thanks
Your code appears to be AS2 rather than AS3?
If your looking for AS3 functionality, you'll want to do something like listen to the stage for keyboard events, and then have an event handler that progresses the frame.
stage.addEventListener(KeyboardEvent.KEY_DOWN,_handleKeyboardPress);
function _handleKeyboardPress(e:KeyboardEvent)
{
nextFrame();
}
Your code looks as though it would work if you are publishing to AS2 though?

Touch movement will not animate

Ok, so I'm in the process of developing a game in Adobe Flash (air) it will be a mobile game.I asked this question a while ago, but animation doesn't work so know I'm back.Anyway, the problem I was having was that there was no continuous movement when I held down a button, so I had to constantly keep pressing the button to move.the person told me to use ticks, so it's how I built the movement engine, all around ticks.When I go to add my animation, it goes once and just stops (the animation) here is my code:
myButton.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
function mouseDown(e:Event):void {
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp); //listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.
addEventListener(Event.ENTER_FRAME,tick); //while the mouse is down, run the tick function once every frame as per the project frame, this is where I add the players animation.
}
function mouseUp(e:Event):void {
removeEventListener(Event.ENTER_FRAME,tick); //stop running the tick function every frame now that the mouse is up
stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp); //remove the listener for mouse up
}
function tick(e:Event):void {
//do your movement
}
What I'm saying is, is there anyway to get around the problem? How can I animate with the tick!
Like others, I find the code perfectly fine. Since, I don't have access to entire code and timeline, I have the below guess about the problem:
Since, you want to start animation only on mouse down, then you must be having a stop() action in the first frame of your animation. (Please correct me if this isn't the case). The last frame of your animation must be having gotoAndPlay(x) action again for looping.
Based on above assumption, when your animation loops, it again reaches a frame which has stop() action already defined.

Flash AS3: Keeping the mouse within certain boundaries

So this one is a tricky one (for me) vital to the development of my project due to the fact that we can't directly modify the position of mouseX and mouseY - they are read-only variables.
Basically, what I want to do is have a player able to move their mouse only within a certain triangular area when a specific instance is active. The latter bit I can manage just fine, however I am having trouble restricting mouse movement -- or apparent mouse movement.
Here's what I have done so far:
1. Assign a library moveclip to the mouseX and mouseY position in the Event.ENTER_FRAME event - although I acknowledge that this should be moved to Mouse.MOUSE_MOVE. (this does not matter yet)
2. Using Corey O'Neils Collision detection kit, do a hit test on the border instances of the area with the crosshair/cursor.
3. Offset the cursor appropriately, and then set a standard Boolean value to false so that the cursor will not keep bouncing back into the cursor over and over.
My problem is, I am not sure what the best way is to go about allowing mouse movement again. Can anyone give me some tips on the best way to do this, or if necessary, point me in another direction where restricting mouse movement is a little easier?
For what it's worth, this is to stop users from aiming in an unrealistic direction with a character in a top-down (ish) shooter.
For those unfamiliar with Corey O'Neil's Collision Detection Kit, I believe it is just a pre-built setup of bitmap (or maybe vector) collision testing - I could be wrong. I'm not sure on the details of how it works, just its basic implementation.
Here is my code regarding mouse movement thus far:
import flash.ui.Mouse;
import flash.events.event
import com.coreyoneil.collision.CollisionList;
Mouse.hide();
var c:crosshair = new crosshair();
addchild(c);
var myCollisionList:CollisionList;
myCollisionList = new CollisionList(c); //sets up detection for the object c
myCollisionList.addItem(mcB); // adds mcB to the list of objects to check c's hittest with
function aim(e:Event) {
var collisions:Array = myCollisionList.checkCollisions();
if (collisions.length>0)
{
hashit = true; // tells the program that the mouse has collided with a boundary
c.x += 1;
c.y += 1;
}
else
{
if (hashit == false)
{
c.x = mouseX;
c.y = mouseY;
}
}
}
Apologies for the code block, but I figure it is best to show all relevant code -- I'm not sure about the complexity of this issue due to the read-only nature of the mouse's X and Y position.
Also, I'm looking for a possible solution which will not be clunky - that is, as soon as the mouse is back in the area, mouse movement will be smooth as it is originally, and where the cursor will still be matching the mouse position (meaning, the cursor is ALWAYS relevant to the mouse and will not change position should the mouse leave the boundaries).
Could anyone please give me some pointers? Sorry for the long question. I gather there might be a bit to get my head around here, being relatively new to AS3 - but I still feel this is a problem I can get past, if one of you can show me the right direction and help me with both the logic and programming side of things slightly.
Here is a diagram of my stage to clarify the boundary areas etc.
Thanks very much for any help in advance, I really do appreciate it!
Cheers, Harry.
How about trying getObjectsUnderPoint which returns an array of objects under a certain point.
If your triangle object is within the array the cursor must be above it.
var pt:Point = new Point(c.x, c.y);
var objects:Array = stage.getObjectsUnderPoint(pt);
if (objects.indexOf(triangleObject) > -1) {
trace("still within bounds");
}
The workaround here could be to hide the system mouse cursor and add a bespoke cursor movieclip to the stage.
Using a MOUSE_MOVE event listener attached to the stage, set the bespoke cursor movieclip to match the stage.mouseX and stage.mouseY values and also test whether the movieclip is outside your bounds. If so, set it back within your bounds.

How to handle mouseEvent transparently in AS3?

I have a DisplayObject docked at the top of my interface that displays debug information (frames per second, etc.) and is translucent with an alpha of 60%.
I would like to interact with items under this surface, such that when the mouse rolls over it, it dims to 10% alpha, and mouse events pass through it to the underlying objects.
Normally, I have this debug info panel's mouseEnabled and mouseChildren properties set to false, so objects under it receive mouse events.
The problem is that in order to hide it when the mouse rolls over it, it needs to have mouseEnabled set to true. However, if mouseEnabled is true, the mouse events are not picked up by objects underneath it.
As far as I know, I can't selectively enable mouseEvents, so it's either going to receive them all or none of them. That means that I'd have to handle and forward ALL events, if I took that approach.
I really wish the mouseEnabled property had a "peek" mode or something, so that it could receive the events if it is on top, but also allow them to pass through to objects underneath.
If a DisplayObject has mouseEnabled=true it means that its events will be sent to its container not to whateve is underneath the object. So this solution will not work. The best solution would be to reroute events from it manually using getObjectsUnderPoint as described here.
I've been using this approach for years in multi-touch apps. With multiple touch points I don't see any processor overhead. And you got only one cursor.
I feel your pain. Unfortunately, I don't know of a way to enable/disable specific mouse events. You could get creative with the solution though. For instance, maybe try adding a MOUSE_MOVE listener to your stage and track the coordinates of the mouse. Then, if the stageX,stageY of the mouse is in the area of your panel, set the visibility. You might also be able to use getObjectsUnderPoint() to determine which objects are under the mouse. But, my guess is that it would get a little intense on the processor to run that on each frame iteration.
I believe you are looking for mouseEnabled = false
But another last ditch attempt you can do is on mouse over move it to the other side of the screen.
One approach you can take, although not ideal, is to add an enter frame listener and check the mouse position every frame. something along the lines of:
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
private function onEnterFrame(e:Event):void {
if(mouseX > width || mouseY > height){
//hide stats
}
}
I'm assuming you have this display hierarchy:
Debug Window
Debug Control 1
Debug Control 2
...
Overlay
Why not make the overlay a mask on DebugWindow and have your mouseEvents attached to DebugWindow itself? See this page for some inspiration: http://blog.shaperstudio.com/2010/11/as3-inverse-mask/
I had this same problem.. i made function to check is mouse over certain object:
public function isMouseOverObject(mPos: Point, pObject:DisplayObject, pContainer:DisplayObjectContainer) {
var under_objects:Array = pContainer.getObjectsUnderPoint(mPos);
var is_under:Boolean = false;
for (var i:int = 0; i < under_objects.length; i++) {
if (under_objects[i] == pObject) {
is_under = true;
break;
}
}
return is_under;
}