How do i update text graphics in flashpunk? - actionscript-3

Here is a class i made that will be used to make the items for a menu i'm working on.
package
{
import net.flashpunk.Entity;
import net.flashpunk.graphics.Graphiclist;
import net.flashpunk.graphics.Stamp;
import net.flashpunk.graphics.Text
import net.flashpunk.FP;
import net.flashpunk.utils.Input;
public class MenuItem extends Entity
{
public var label:Text; //public text var to display label
public var amount:Text;
public var amountText:String;
public function MenuItem(x:Number=0, y:Number=0, text:String = "", amnt:Number = 0)
{
super(x, y);
label = new Text(text, 10, 10, { size: 20, color: 0xFFFFFF, width: 111, wordWrap: false, align: "center" } ); //format text
label.y = 25; //place text
label.x = 0; // place text
amountText = amnt.toString();
amount = new Text(amountText, 10, 10, { size: 20, color: 0xFFFFFF, width: 111, wordWrap: false, align: "center" } );
amount.y = 25;
amount.x = 50;
graphic = new Graphiclist(label, amount); //set graphic list (png and text)
setHitbox(110, 70, 0, 0);
}
override public function update():void
{
}
}
}
What i'm looking for is how to update the amnt using the update function.
But my knowledge of flashpunk is not great and i;m not sure how i would go about doing this exactly. please and thank you! if what i'm asking is unclear please contact me so i can specify whatever is needed.

Related

Shape not rendering on HTML 5 canvas when using non-hardcoded parameters

I'm trying to create a basic HTML 5 game using typescript but I have run into trouble with rendering a circle of a size that is non hard-coded.
My code so far:
Circle.ts
import { IDrawable } from './../Interfaces/IDrawable';
import {BaseShape} from "./BaseShape";
export class Circle extends BaseShape implements IDrawable{
private _radius : number;
public get radius(){
return this._radius;
}
public set radius(val : number){
this._radius = val;
}
constructor(context : CanvasRenderingContext2D,radius : number, x : number, y : number, color : string = "red", lineWidth : number = 2) {
super(context , x, y, color, lineWidth);
}
public draw = () : void => {
this.context.save();
this.context.beginPath();
this.context.strokeStyle = this.color;
this.context.lineWidth = this.lineWidth;
this.context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
this.context.stroke();
this.context.restore();
}
}
Game.ts
import { Circle } from './Shapes/Circle';
export default class Game{
private circle : Circle;
constructor(private ctx : CanvasRenderingContext2D ){
this.circle = new Circle(ctx, 200, 300, 50, "blue", 5);
}
public Start() : void{
requestAnimationFrame(() => this.Start());
this.ctx.fillStyle = "black";
this.ctx.fillRect(0, 0, 1280, 720);
this.circle.draw();
}
}
The background renders fine but the circle won't render at all unless I change the line:
this.context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
and hard-code the parameters.
Not sure what the problem is, tried both using an arrow function for draw() and using a normal method, also tried supplying the context as a method parameter for draw() with the same result.
The problem is that you are passing wrong argument to constructor of Circle. Se parameters of constructor and values you pass.
context: CanvasRenderingContext2D ctx
radius: number 200
x: number 300
y: number 50
color: string "blue"
lineWidth: number 5
I think you have switched the order of radius and coordinates parameter.

Recieveing Error : undefined property bitmapfilter type AS3

I'm working on a tutorial for a pong clone and am currently trying to add a BevelFilter to a ball image. When I type in the code it returns an error saying
undefined property bitmapfilter type
I checked on live docs and it should be valid. I marked the line with two *'s
package
{
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;
import flash.filters.BevelFilter;
import flash.filters.GlowFilter;
public class Ball extends Sprite
{
private const RADIUS:int = 12;
private const COLOR:uint = 0x01A6B2;
private const COLOR2:uint = 0x45FCFF;
** private const BEVEL:BevelFilter = new BevelFilter(4, 90, COLOR2, 1, COLOR2, 1, 10, 10, 1, 1, BitmapFilterType.Inner, true);
private const GLOW:GlowFilter = new GlowFilter(0xFFFFFF, .6, 0, 0, 5, 1, true);
public function Ball():void {
addEventListener(Event.ADDED_TO_STAGE, go);
}
private function go(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, go);
graphics.lineStyle(2, COLOR, 1);
graphics.beginFill(COLOR);
graphics.drawCircle(0, 0, RADIUS);
filters = [BEVEL, GLOW];
}
}
}
Thanks!
I use these on a regular basis. Your code should look like this...
private const BEVEL:BevelFilter = new BevelFilter(4, 90, COLOR2, 1, COLOR2, 1, 10, 10, 1, 1, "inner", true);
That second-to-last argument accepts a string.
Not sure why the docs represent it differently. This was off of the code hints for Adobe Flash CS6, and I know this should work.

Trying to essentially push details through classes

I am working on a game that creates three circles" red, green, and blue who have 3,2,1 health respectively. They get removed from the stage after their health reaches 0 and it is decremented by 1 per click. I am using a Main.mxml file then I have a Target.as file, as well as RedTarget.as, GreenTarget.as, and BlueTarget.as. My question is that I would like to set everything up in my Target.as file, then push the details such as color, health, and if they are dead or not through those functions. I am having trouble doing that though because I am not sure what I would need in the Target.as and then what I would need to code in each of the colored target files.
Here is my Target.as file:
package com.multiClicker {
//import the needed classes
import flash.display.Shape;
import flash.events.MouseEvent;
import spark.components.Image;
public class Target extends Image {
public function Target() {
//add event listeners
this.addEventListener(MouseEvent.CLICK, onClick);
}
//sets the hp of the target
public function hp():Number {
return hp;
}
//get function that returns false if alpha is <= 0
public function dead():Boolean {
if(alpha <= 0){
return false;
}
return true;
}
//subtracts one from targets HP when clicked
public function onClick(e:MouseEvent = null):void {
//subtracts one from hp each click
hp --;
if(hp <=0) {
this.addEventListener(onEnterFrame);
}
}
//subtracts .1 from the classes alpha
public function onEnterFrame():void{
this.alpha =- .1;
}
//draws the target
public function drawTarget(color):void {
var circle:Shape = new Shape();
circle.graphics.beginFill(color);
circle.graphics.drawCircle(0,0,30);
}
}
}
and then my RedTarget.as file, which is the same as blue and green, except for that they are labeled as such in the variables:
package com.multiClicker {
import flash.events.MouseEvent;
public class RedTarget extends Target{
private var redHP:Number = 3;
private var redDead:Boolean = false;
private var redColor:String = "red";
public function RedTarget()
{
redHP = hp;
redDead = dead;
redColor = color;
//include the super function
super();
}
//subtracts one from targets HP when clicked
override public function onClick(e:MouseEvent=null):void {
super.onClick(e);
//push all to super
}
}
}
Any help on the issue would be great. I have been trying to figure it out throughout the day but have not figured it out.
Are you just asking how to pass variables in when you create the Target object?
public class Target extends Image {
public function Target(hp:Number, dead:Boolean, color:String) {
this.hp = hp;
this.dead = dead;
this.color = color;
}
}
Then you instantiate each target like this:
var redTarget:Target = new Target(3, false, "red");
var greenTarget:Target = new Target(2, false, "green");
etc...

Why the styleSheet is not working?

package {
import flash.text.TextField;
import flash.text.StyleSheet;
import flash.text.TextFieldAutoSize;
public class MyTF extends TextField {
public var
color:uint,
size:int
public function MyTF( color:uint, size:int) {
super();
color = color; // setting color and size
size = size;
init(); //initialize
}
private function init():void
{
autoSize = TextFieldAutoSize.LEFT;
var style:StyleSheet = new StyleSheet();
style.setStyle('span', {color: 0xFF0000, fontSize: 24});
styleSheet = style;
htmlText = '<span>test</span>';
//after creating an object of this class the text Style is not changing
}
}
}
Why is the htmlText style not changing after I create an instance of this class? Is there something wrong?
If I'm not wrong understand you this can help you:
style.setStyle('span', {color: 0xFF0000, fontSize: 24}); change to
style.setStyle('span', {color: this.color, fontSize: this.size});.

ActionScript Read Sprite's Graphics?

how can i retrieve the set graphics properties of a sprite? for example, in the code below, i'd like to get the color of the sprite, also it's rounded corners setting and other graphics attributes.
var sp:Sprite = new Sprite();
sp.graphics.beginFill(0xFF0000, 0.75);
sp.graphics.drawRoundRect(0, 0, 300, 50, 10, 10);
sp.graphics.endFill();
addChild(sp);
trace(sp.graphics.color); //pseudo trace - this doesn't work
I am almost certain this is not possible. However, there are certainly other ways to do this. What about having a valueObject for each property that stores the values used. Then you could have a GraphicalDisplayObject that you either inherit from or use via composition. For instance:
package {
class FillVO extends Object {
public var fill_color:Number = 0xFF0000;
public var fill_opacity:Number = 0.75;
}
}
package {
import FillVO;
class GraphicalDisplayObject extends Sprite {
public var fill_vo:FillVO;
public function beginFill($vo:FillVO) {
graphics.beginFill($vo.fill_color, $vo.fill_opacity);
}
...
}
}
package {
import GraphicalDisplayObject;
class ObjectWithGraphicalProperties extends Sprite {
public var gfx:GraphicalDisplayObject;
public function ObjectWithGraphicalProperties() {
gfx = new GraphicalDisplayObject();
addChild(gfx);
}
public function beginFill($color:Number, $opactity:Number) {
var fill_vo:FillVO = new FillVO();
fill_vo.fill_color = $color;
fill_vo.fill_opacity = $opacity;
gfx.beginFill(fill_vo);
}
...
}
}
Then to use it...
var obj:ObjectWithGraphicalProperties = new ObjectWithGraphicalProperties();
addChild(obj);
obj.beginFill(0xffff00, .2);
...
...
trace(obj.gfx.fill_vo.fill_color);
This is obviously via composition, and you would need to write the additional proxied methods and corresponding valueObjects... but it should work.