Skip to main content
Inspiring
October 13, 2013
Answered

How to change cursor on roll over function

  • October 13, 2013
  • 2 replies
  • 1222 views

I'm making a point and click game in AS3 with flash.

I've changed the skin of my cursor by creating a new class "Souris". It's working just fine. Now I'm trying to change the skin of the cursor when it's on an object on the scene.

I've read that the MouseEvent.ROLL_OVER is the good method but I can't figure out how to do it...

I've got my Souris class like that :

public class Souris extends MovieClip
   
{
private var engine:Engine;
       
private var stageRef:Stage;
       
private var p:Point = new Point();

       
public function Souris(stageRef:Stage)
       
{
           
Mouse.hide(); //make the mouse disappear
            mouseEnabled
= false; //don't let our cursor block anything
mouseChildren
= false;

           
this.stageRef = stageRef;
            x
= stageRef.mouseX;
            y
= stageRef.mouseY;

            stageRef
.addEventListener(MouseEvent.MOUSE_MOVE, updateMouse, false, 0, true);
            stageRef
.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler, false, 0, true);
            stageRef
.addEventListener(Event.ADDED, updateStack, false, 0, true);
            stageRef
.addEventListener(MouseEvent.ROLL_OVER,hover);

       
}

       
private function updateStack(e:Event) : void
       
{
            stageRef
.addChild(this);
       
}
       
private function hover(e:MouseEvent😞void {
souris
.visible = false;
}

       
private function mouseLeaveHandler(e:Event) : void
       
{
            visible
= false;
           
Mouse.show(); //in case of right click
            stageRef
.addEventListener(MouseEvent.MOUSE_MOVE, mouseReturnHandler, false, 0, true);
       
}

       
private function mouseReturnHandler(e:Event) : void
       
{
            visible
= true;
           
Mouse.hide(); //in case of right click
            removeEventListener
(MouseEvent.MOUSE_MOVE, mouseReturnHandler);
       
}

       
private function updateMouse(e:MouseEvent) : void
       
{
            x
= stageRef.mouseX;
            y
= stageRef.mouseY;

            e
.updateAfterEvent();
       
}

   
}

}
}

In my main class (Engine class) I've got :


private var souris:Souris;

public function Engine(){



                        souris
= new Souris(stage);
            stage
.addChild(souris);

       
}
private function startGame(e:Event😞void{
....
..

I've tried to put in the "Souris" class


stageRef.addEventListener(MouseEvent.ROLL_OVER,hover);

private function hover(e:MouseEvent😞void {
Engine.souris.visible = false;
handCursor
.visible = true ;
}

But It seems wrong... I don't know what to put in my hover function.. (I've got the "handCursor" in my library).

Thank you very much for your help !

This topic has been closed for replies.
Correct answer Stephdidou

Thx for your answers, I've found a solution to my problem. It was MouseEvent.MOUSE_OVER. and not roll over. But thanks for the links, I've read it and it helps me with some codes.

Thanks for your time !

2 replies

Inspiring
October 14, 2013

Don`t bother with writing your own cursor from the ground up, you are in for trouble, especially when you make a game with dynamicly created objects ( for example:I see no sorting algorithm that gurantees that your custom cursor stays in front of all other objects at all times).

Use sth like this: http://asgamer.com/2009/an-even-better-as3-oop-flash-custom-cursor

StephdidouAuthorCorrect answer
Inspiring
October 14, 2013

Thx for your answers, I've found a solution to my problem. It was MouseEvent.MOUSE_OVER. and not roll over. But thanks for the links, I've read it and it helps me with some codes.

Thanks for your time !

Ned Murphy
Legend
October 13, 2013

If you want to have rolling over objects trigger something to occur relative to the souris object, then you need to assign the ROLL_OVER and ROLL_OUT event listeners to the objects that get rolled over.  You can have the event handler functions for those call upon the souris object to execute whatever function it executes for the cursor change you want to have happen.

main_obj.addEventListener(MouseEvent.ROLL_OVER, changeCursor);

function changeCursor(evt:MouseEvent):void {

         souris.showRolloverCursor();

}