Keeping the text on the screen?


(John Andersson) #1

Hi. I am displaying text when the mouse hovers above some buttons.

package Talents
{
import net.flashpunk.Entity;
import net.flashpunk.Graphic;
import net.flashpunk.graphics.Graphiclist;
import net.flashpunk.graphics.Text;
import net.flashpunk.Mask;
import net.flashpunk.graphics.Stamp;
import net.flashpunk.utils.Input;
import net.flashpunk.FP;
import flash.geom.Point;

public class Agility_Dagger extends Entity 
{
	protected var normal:Graphic;
	protected var hover:Graphic;
	protected var down:Graphic;
	protected var clicked:Boolean = false;
	protected var label:Text;
	protected var normalStamp:Stamp = new Stamp(Assets.AGILITY_DAGGER);
	protected var text:String = "Dagger \n Testtesttesttest."

	public function Agility_Dagger(p:Point) 
	{
		x = p.x;
		y = p.y;
		
		//Assign the button graphic states to variables
		normal = normalStamp;
		hover = new Stamp(Assets.AGILITY_DAGGER_HOVER);
		down = new Stamp(Assets.AGILITY_DAGGER_DOWN);
		
		graphic = normal;
		
		setHitboxTo(normalStamp);
	}
	
	override public function update():void
	{
		super.update();
		
		if (graphic != normal)
		{
			label = new Text(text, 10, 70, { size: 30, color: 0xFFFFFF, width: normalStamp.width - 30, align: "center" } );
		}else
		{
			label = null;
		}
		
		//If the mouse is colliding with this button
		if (collidePoint(x, y, world.mouseX, world.mouseY))
		{
			if (Input.mousePressed) clicked = true;
			
			if (clicked) graphic = down;
			else graphic = hover;
			
			if (clicked &&Input.mouseReleased) 	click();
		}
		else
		{
			if (clicked) graphic = hover;
			else graphic = normal;
		}
		
		//If mouse was released
		if (Input.mouseReleased) clicked = false;
	}
	
	protected function click():void
	{
		trace("click");
	}
	
	
	override public function render():void
	{
		super.render();
		 
		renderGraphic(label);
	}
	
	protected function renderGraphic(graphic:Graphic):void
	{
		if (graphic && graphic.visible)
		{
			if (graphic.relative)
			{
				_point.x = x;
				_point.y = y;
			}
			else _point.x = _point.y = 0;
			_camera.x = world ? world.camera.x : FP.camera.x;
			_camera.y = world ? world.camera.y : FP.camera.y;
			graphic.render(renderTarget ? renderTarget : FP.buffer, _point, _camera);
		}
	}
	
	protected var _point:Point = FP.point;
	protected var _camera:Point = FP.point2;
}

}

But if the button is too far to the right, the entire text isn’t visible. How do I make it always show the entire text, no matter where the button is? :stuck_out_tongue: Thanks


(Bora Kasap) #2

if you have a “_point” like that, and if you’re using “center” for text align then you can use

FP.clampInRect(_point, textWidth/2, 0, FP.width, FP.height - textWidth/2);

(John Andersson) #3

Hmm, interesting. I have no idea where to put it though xD


(Bora Kasap) #4

You probably need something like that next to your label = new Text("asdasd");

FP.clampInRect(label, label.width/2, label.height/2, FP.width + label.width/2, FP.height - label.width/2);

(John Andersson) #5

Hmm… I added it to one of the buttons whose text is appearing outside of the screen (half of it). I changed the button to this

package Talents { import net.flashpunk.Entity; import net.flashpunk.Graphic; import net.flashpunk.graphics.Graphiclist; import net.flashpunk.graphics.Text; import net.flashpunk.Mask; import net.flashpunk.graphics.Stamp; import net.flashpunk.utils.Input; import net.flashpunk.FP; import flash.geom.Point; import game_handling.Pause;

public class Agility_Dagger extends Entity 
{
	protected var normal:Graphic;
	protected var hover:Graphic;
	protected var down:Graphic;
	protected var clicked:Boolean = false;
	protected var label:Text;
	protected var normalStamp:Stamp = new Stamp(Assets.AGILITY_DAGGER);
	protected var text:String = "Dagger Prance \n Your jumping abilities benefit from equipped daggers. Increases jumping efficiency by 25% if daggers are equipped."

	public function Agility_Dagger(p:Point) 
	{
		x = p.x + FP.world.camera.x;
		y = p.y + FP.world.camera.y;
		
		//Assign the button graphic states to variables
		normal = normalStamp;
		hover = new Stamp(Assets.AGILITY_DAGGER_HOVER);
		down = new Stamp(Assets.AGILITY_DAGGER_DOWN);
		
		graphic = normal;
		
		setHitboxTo(normalStamp);
	}
	
	override public function update():void
	{
		//Text
		
		if (collidePoint(x, y, world.mouseX, world.mouseY))
		{
			label = new Text(text, 10, 70, { size: 30, color: 0xFFFFFF, width: normalStamp.width - 30, align: "center" } );
			FP.clampInRect(label, label.width/2, label.height/2, FP.width + label.width/2, FP.height - label.width/2);
		}else {
				label = null;
		}
		
		if (Upgrades.agility_dagger_purchased == false)
		{
			if (graphic != normal)
			{
				label = new Text(text, 10, 70, { size: 30, color: 0xFFFFFF, width: normalStamp.width - 30, align: "center" } );
			}else
			{
				label = null;
			}
			
			//If the mouse is colliding with this button
			if (collidePoint(x, y, world.mouseX, world.mouseY))
			{
				if (Input.mousePressed) clicked = true;
				
				if (clicked) graphic = down;
				else graphic = hover;
				
				if (clicked &&Input.mouseReleased) 	click();
			}
			else
			{
				if (clicked) graphic = hover;
				else graphic = normal;
			}
			
			//If mouse was released
			if (Input.mouseReleased) clicked = false;

		}else {
			graphic = down;
		}
		
		if (Pause.PAUSED == false)
		{
			FP.world.remove(this);
		}
	}
	
	protected function click():void
	{
		if (Upgrades.agility_dagger_purchased == false)
		{
			if (HeroStats.talentPoints >= 1)
			{
				HeroStats.talentPoints --;
				HeroStats.jumpPower *= 1.25;
				Upgrades.agility_dagger_purchased = true;
			}
		}
	}
	
	
	override public function render():void
	{
		super.render();
		 
		renderGraphic(label);
	}
	
	protected function renderGraphic(graphic:Graphic):void
	{
		if (graphic && graphic.visible)
		{
			if (graphic.relative)
			{
				_point.x = x;
				_point.y = y;
			}
			else _point.x = _point.y = 0;
			_camera.x = world ? world.camera.x : FP.camera.x;
			_camera.y = world ? world.camera.y : FP.camera.y;
			graphic.render(renderTarget ? renderTarget : FP.buffer, _point, _camera);
		}
	}
	
	protected var _point:Point = FP.point;
	protected var _camera:Point = FP.point2;
}

}

As you can see, I tried implementing your “FP.clampInRect(label, label.width/2, label.height/2, FP.width + label.width/2, FP.height - label.width/2);”, but it doesn’t work. The text is still displaying some of itself outside of the screen