Skip to main content
Participating Frequently
February 6, 2021
Answered

need help Converting Actionscript to HTML5 in Adobe Animate using random

  • February 6, 2021
  • 1 reply
  • 931 views

this is my code for Actionscript:

 

//for creating a hexagon on click

 

stage.addEventListener(MouseEvent.CLICK, addHex);
function addHex(e:MouseEvent):void {
var newHex: hex1 = new hex1
var size:int = Math.round(Math.random()*50+10);
this.addChildAt(newHex, 1);
newHex.x= mouseX;
newHex.y= mouseY;
newHex.width= size;
newHex.height= size;
}

 

How could I convert this to HTML5?

THANK YOU IN ADVANCE

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    https://shared-assets.adobe.com/link/8a1e0f2a-d854-4d7d-59e1-cbb5650b6232


    Thanks.

     

    There are multiple instances that are interfering with the mouse click.

     

    One solution will be to place an invisible button at the top that covers the entire canvas and then add the click listener to the button instead of adding to the stage.

     

    The code would be slightly modified:

     

    function addHex(e)
    {
    	var newHex = new lib.hex1();
    	var size = Math.round(Math.random() * 50 + 10);
    	
    	newHex.x = stage.mouseX / stage.scaleX;
    	newHex.y = stage.mouseY / stage.scaleY;
    	newHex.scaleX = size / newHex.nominalBounds.width;
    	newHex.scaleY = newHex.scaleX;
    	
    	this.addChild(newHex);
    }
    
    this.hit.cursor = "none"; // hit is the instance name of the invisible button
    this.hit.on("click", addHex, this);

     

     

    I hope it works.

    1 reply

    JoãoCésar17023019
    Community Expert
    Community Expert
    February 6, 2021

    Hi.

     

    Like this:

    function addHex(e)
    {
    	var newHex = new lib.hex1();
    	var size = Math.round(Math.random() * 50 + 10);
    	
    	newHex.x = stage.mouseX / stage.scaleX;
    	newHex.y = stage.mouseY / stage.scaleY;
    	newHex.scaleX = size / newHex.nominalBounds.width;
    	newHex.scaleY = newHex.scaleX;
    	
    	this.addChildAt(newHex, 1);
    }
    
    stage.on("click", addHex, this);

     

    Besides the code, you will need to set the linkage name of your symbol in the Library to hex1 and you will also need to have some shape, bitmap or symbol instance in the background to serve as a hit area so the click event will be fired.

     

     

    I hope this helps.

     

    Regards,

    JC 

    AmarisGilAuthor
    Participating Frequently
    February 6, 2021

    Thank you for replying, 

     

    The instance of the hexagon that is supposed to appear is called "hex1" and it shows in the linkage in my library. 

    Although, the hexagons still don't appear when I click.

    The background is called "back_mov" and the instance is called "back" although in my library it shows no linkage.

     

    Additional information that might be helpful to solve this problem:

    I created a cursor called "mouse" with the instance of "hex3" although in my library it shows no linkage.

     

    -Amaris

     

    AmarisGilAuthor
    Participating Frequently
    February 6, 2021

    It seems like the hexagons do appear, but only on a specific area, this area is called "body_mov"