Adding a link into the game[Solved]


(billy2000) #1

Hello guys. I’ve been thinking about this a lot lately.So i want to add a link to a site into the game. The problem is that if i use a code about this in update function like this

navigateToURL(new URLRequest("link"), "_blank");

The browser take it as a popup and block it(so i need to click allow on the browser to actually get into the link)

And if i use a event listener like this:

//in constructor
FP.stage.addEventListener ( MouseEvent.CLICK, TheLink ) ;
//the event listener
public function TheLink (e:Event):void
	{
		if (collide("cursor", x, y) )
		navigateToURL(new URLRequest("link"), "_blank");
	}

Sometimes i get a bug that everywhere i click it opens the link(not blocked this time tho cause it doesn’t wait until the end of the frame to open it). Any ideas of a working code for adding a link into the game?


(Ultima2876) #2

The way to do it is in the mouse release event. I chain 'em up like so:

//in constructor
FP.stage.addEventListener ( MouseEvent.MOUSE_DOWN, OnMouseDown);

//released listener
public function OnMouseDown(e:Event = null): void
{
  if (collide("cursor", x, y) )
  {
    FP.stage.addEventListener( MouseEvent.MOUSE_UP, TheLink);
  }
}

//the event listener
public function TheLink(e:Event):void
{
  if(FP.stage.hasEventListener(MouseEvent.MOUSE_UP)
  {
    FP.stage.removeEventListener(MouseEvent.MOUSE_UP, TheLink);
  }
  
  if (collide("cursor", x, y) )
  navigateToURL(new URLRequest("link"), "_blank");
}

(billy2000) #3

Thank you very much! Ur a lifesaver xD


(Darrin) #4

This looks very useful but I’m confused where does this go in the code?

I can’t seem to use FP.stage.addEventListener in a World or Entity as I get a null object reference. Where does this code get added?


(Jacob Albano) #5

Try putting it in the init() override in your main class (the one that extends Engine).


(Darrin) #6

Perfect thanks! I had to change the collide a bit but worked!