problem with graphic sprite inside a MovieClip Class

Contributor ,
Feb 16, 2021 Feb 16, 2021

Copy link to clipboard

Copied

¡Hola!.

Hello!.

I have a MovieClip "panelMenu" class.
Inside the class he drew a rectangle (cap) to use later in an event.

What happens is that I do not see the rectangle! No matter how much I put it in one place or another, it can't be seen! This code works perfectly in the action panel, but when passing it to a class I cannot make the rectangle display.

addChild does not work and there is no compiler error.

 

 

 

Thanks for the help

 

 

package componentes {

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.Sprite;


public class panelMenu extends MovieClip {

var tapa:Sprite;

public function panelMenu() {
tapa = new Sprite();
tapa.graphics.beginFill(0x8A9597, 1);
tapa.graphics.drawRect(0,0,180,800);
tapa.graphics.endFill();
tapa.alpha = 0.8;
function playMenu (event:MouseEvent):void{
if (currentLabel == "uno"){
play();
addChild(tapa);
}
}
function retryMenu (event:MouseEvent):void{
if (currentLabel == "diez"){
play();
removeChild(tapa);

}
}
addEventListener (MouseEvent.CLICK, playMenu);
tapa.addEventListener (MouseEvent.CLICK, retryMenu);
}


}

}

 

 

 

 

 

 

 

 

Views

81

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Contributor , Feb 18, 2021 Feb 18, 2021
Hello! I don't quite understand what you mean ... could you help me explain it better?I have put this in the action panel. So I instantiate the PanelMenu class and call the Sprite, but it doesn't come outThank you var pepe:panelMenu = new panelMenu(); pepe.tapa;

Likes

Translate

Translate
Contributor , Mar 02, 2021 Mar 02, 2021
Thanks a lot!

Likes

Translate

Translate
Adobe Community Professional ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

Hi.

 

It's because you're not adding the sprite instance to the Display List when panelMenu is instantiated. If you don't do this, your click event won't work because your panelMenu don't have any graphic to detect a mouse event.

package componentes
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.display.Sprite;

	public class panelMenu extends MovieClip
	{
		var tapa:Sprite;

		public function panelMenu()
		{
			tapa = new Sprite();
			tapa.graphics.beginFill(0x8A9597, 1);
			tapa.graphics.drawRect(0, 0, 180, 800);
			tapa.graphics.endFill();
			tapa.alpha = 0.8;
			addChild(tapa);			
			addEventListener(MouseEvent.CLICK, playMenu);
			tapa.addEventListener(MouseEvent.CLICK, retryMenu);
		}
		
		private function playMenu(event:MouseEvent):void
		{
			if (currentLabel == "uno")
			{
				play();
				addChild(tapa);
			}
		}
		
		private function retryMenu(event:MouseEvent):void
		{
			if (currentLabel == "diez")
			{
				play();
				removeChild(tapa);
			}
		}
	}
}

 

Also, it's a better practice to define your methods outside the constructor.

 

Please let us know if you have any further questions.

 

Regards,

JC

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

It seems that now it comes out but I leave the thread open because I have to check more times. I have not instantiated, but it comes out. In a few days I continue.
Thanks

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

Hello! I don't quite understand what you mean ... could you help me explain it better?

I have put this in the action panel. So I instantiate the PanelMenu class and call the Sprite, but it doesn't come out

Thank you

 

 

var pepe:panelMenu = new panelMenu();
pepe.tapa;

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

Hi.

 

The panelMenu is a display object because it extends the MovieClip class. So you must add any instance of it to the display list first for it to work as expected. Like this:

var pepe:panelMenu = new panelMenu();
addChild(pepe);

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

LATEST

Thanks a lot!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines