Skip to main content
hartz
Inspiring
February 13, 2018
Answered

Trying to load a library image with toggle button

  • February 13, 2018
  • 1 reply
  • 416 views

This is the AS3 for working the button that I created and it works fine.  However, I want something to happen and I put in the // in this code for what I need help with...

import flash.events.MouseEvent;

toggleButton.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);

toggleButton.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);

toggleButton.addEventListener(MouseEvent.CLICK, toggleClick);

toggleButton.buttonState = "on";

function rolloverToggle(event:MouseEvent) {

    toggleButton.gotoAndStop(toggleButton.buttonState+ "over");

  

}

function rolloutToggle(event:MouseEvent) {

    toggleButton.gotoAndStop(toggleButton.buttonState);

}

function toggleClick(event:MouseEvent) {

    if (toggleButton.buttonState == "on"){

        toggleButton.buttonState = "off";

        //I want to load a png from library called MyImage1 to 0,0

    }else {

        toggleButton.buttonState = "on";

        //I want to remove png called MyImage1 from stage

    }

        toggleButton.gotoAndStop(toggleButton.buttonState+ "over");

}

If there is an easier way to handle this, that's great.  I just don't know if I need anything else to do this.  I tried addChild, but not as though I know what I'm doing.  In AS2 I didn't have this problem.

This topic has been closed for replies.
Correct answer hartz

THANK YOU.  Worked PERFECT and I learned something as well.

1 reply

JoãoCésar17023019
Community Expert
Community Expert
February 13, 2018

Hi.

This should do it:

import flash.events.MouseEvent;

import flash.display.BitmapData;

import flash.display.Bitmap;

var bitmap:Bitmap;

toggleButton.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);

toggleButton.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);

toggleButton.addEventListener(MouseEvent.CLICK, toggleClick);

toggleButton.buttonState = "on";


function rolloverToggle(event:MouseEvent)

{

    toggleButton.gotoAndStop(toggleButton.buttonState + "over");

}

function rolloutToggle(event:MouseEvent)

{

    toggleButton.gotoAndStop(toggleButton.buttonState);

}

function toggleClick(event:MouseEvent)

{

    if (toggleButton.buttonState == "on")

    {

        toggleButton.buttonState = "off";

      

        bitmap = new Bitmap();

        bitmap.bitmapData = new MyImage1();

        addChild(bitmap);      

    }

    else

    {

        toggleButton.buttonState = "on";

      

        if (bitmap)

            removeChild(bitmap);

    }

  

    toggleButton.gotoAndStop(toggleButton.buttonState + "over");

}

I hope it helps.

Regards,

JC

hartz
hartzAuthorCorrect answer
Inspiring
February 13, 2018

THANK YOU.  Worked PERFECT and I learned something as well.

JoãoCésar17023019
Community Expert
Community Expert
February 13, 2018

This is great!

You're welcome!