AS3: How do i draw shapes in my own class - actionscript-3

I'm very new on AS3. I would like to create a shape as i defined in my own constructor class.
It should create a shape when class was created. (Constructor)
I commented my want in following code :
ballShape class
public class ballShape {
public function ballShape() {
// define shape properties.
// create shape and put that in x = 0, y = 0
}
}
Any helps would be awesome.

You can easily do this while extending your class to Shape or Sprite
Here's your code
public class ballShape extends Sprite {
public function ballShape() {
// define shape properties. The graphics object is already added to your Sprite, no need to manually addChild() this object.
graphics.beginFill(color, alpha); // you can begin a fill with this method, there are also methods to start a bitmap fill, gradient fill.
graphics.drawRect( x, y, width, height ); // draw a shape
graphics.endFill();
}
}
While Shape can have the same functionality to draw shapes and lines, I chose Sprite, because:
You will have interactivity and be able to dispatch events from that class
You will have a set of useful properties that Sprite has.
For more info on the Graphics class, please refer to http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Graphics.html

Related

Post overriding the paint method of the components in java

In java awt or swing when you want to change painting of some component you usually have to override the method paint(Graphics g) (in awt) or paintComponent(Graphics g) (in swing).
This is usually (maybe allways - I'm not sure) done when you are creating the component for example:
JPanel jPanel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//... my implementation of paint, some transfromations, rotation, etc
}
};
Imagine that you have container of components which could for example consists of some JLabels, some JTextFields, some image. Which will be all put on one component.
By container I mean you have some list or map with ids or some similar structure in which are all components you will put on one JFrame.
The question is if I can change the painting method after creating with all of the components which are in this list in the moment when all of them are already created. For example I want do the rotation action (rotate), which is defined in Graphisc2D, with all of them.
So basicaly what I want is that I throught the list of componets I have and say:
"All of you (components) which are in the list will be rotated by some angle". Is that possible? If yes how?
Edit:
This is my not correctly working solution:
graphicalDisplayPanel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g2d = (Graphics2D) g;
g2d.rotate(Math.PI, anchorx, anchory);
}
#Override
public void paintChildren(Graphics g) {
super.paintChildren(g);
Graphics2D g2d2 = (Graphics2D) g;
g2d2.rotate(Math.PI, anchorx, anchory);
}
};
JFrame jFrame = JFrame();
// ... setting dimension, position, visible etc for JFrame, it works correctly nonrotated
jFrame.setContentPane(graphicalDisplayPanel);
I have not tested this, but it seems like it would work. A JComponent's paint() method calls:
paintComponent(co);
paintBorder(co);
paintChildren(co);
where co is a Graphics object. In theory you create an image, retrieve the graphics object and then pass that into paintChildren(). you will have to call paintComponent() and paintBorder() yourself, if you do this. Then, just rotate the image and draw it into your component. You may have to crop the image or resize your component accordingly for this to work. It might look something like this:
BufferedImage myImage;
#Override
public void paint(Graphics g){
myImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TRANSLUCENT);
//using a transparent BufferedImage might not be efficient in your case
Graphics myGraphics = myImage.getGraphics();
super.paintComponent(g);
super.paintBorder(g);
super.paintChildren(myGraphics);
//rotation code here
// ...
//draw children onto your component
g.drawImage(myImage, 0, 0,getWidth(), getHeight(), null);
}
I hope I didn't make any mistakes, please let me know if this works.
So basicaly what I want is that I throught the list of componets I have and say: "All of you (components) which are in the list will be rotated by some angle".
If you want to rotate panel and therefore all the components on the panel as a single using then you need to do the custom painting in the paintComponent() method.
If you want to rotate, for example, individual images that each have a different angle of rotation then you can again do this in the paintComponent(...) method and change the angle for each component.
Or, in this second case you can use the Rotated Icon class. In this case the Icon is just added to a JLabel. Then you can change the degrees of rotation and repaint the label, so there is no custom painting (except in the Icon itself).

SpriteSheet Class AS3

I want to do my own SpriteSheet Class for ActionScript 3.
The purpose of this class would be a replacement for the MovieClip class, the only difference is that the frames will come from a SpriteSheet. Many of my classes will extend from this class.
My problem is: What if I had a sub-class, for example Ball, that uses the same SpriteSheet for all instances of Ball and i would like to re-use that SpriteSheet to use in all balls to save a lot of memory.
If you want to share the same SpriteSheet for all instances of Ball, your Ball class shouldn't extend from SpriteSheet class. The base class should contain a variable of SpriteSheet type, and be initialized when the class is created.
So you need a singleton class to save all the SpriteSheet classes with a dictionary, the key of dictionary may like "Ball", "MAN_RUN".The SpriteSheet class will contain a list of BitmapData.
The code may like this
public class Animation {
private var _spriteSheet:SpriteSheet;
public function Animation(key:String)
{
spriteSheet = SpriteSheetMgr.instance.getSpriteSheet(key);
}
}
private var ball:Ball = new Ball("Ball");

Adding shape to stage as3

I'm not sure if I'm over or under thinking this however I've got this scenario. I wish to be able to add (for now) a triangle to the page using multiple classes. The first one being adding actionscript to a frame in flash the second an actual class. Now, can I do this? Or am I silly thinking I can? I wish to do this so I can create my background as a dynamically created area where thinks I can interact with are placed on as instances of a class.
On frame
import flash.display.Stage;
var sides:Sides=new Sides();
stage.addChild(sides);
this.addChild(sides);
On class
package {
import flash.display.Shape;
public class Sides extends Shape {
public function Sides() {
var triangleHeight:uint = 100;
var triangle:Shape = new Shape();
// red triangle, starting at point 0, 0
triangle.graphics.beginFill(0xFF0000);
triangle.graphics.moveTo(triangleHeight / 2, 0);
triangle.graphics.lineTo(triangleHeight, triangleHeight);
triangle.graphics.lineTo(0, triangleHeight);
triangle.graphics.lineTo(triangleHeight / 2, 0);
triangle.graphics.endFill();
trace("Into construct");
}
}
}
The issue I have is that the actual triangle does not appear on the screen it's blank. I know the constructor is ran, however I get no actual output as such.
I hope I made myself clear. If anyone can suggest a better solution I would love to hear it. My scenario is this.
I wish to create a world that other movie-clips can interact with. I will be creating lines to represent them. Now is it better to do it dynamically generated or is there a way to have some sort of base class that all of the other ones run off where that allows me to have random width. Hope this is clear.
you create an instance of a Shape within your Sides constructor to which you draw the triangle however this shape is never added to a display list, instead you add your instance of Sides (which itself has nothing drawn) to a display list.
Because your Sides class is extending Shape you don't need another instance of a Shape, your instance of Sides itself is a Shape and you can draw directly to it like so:
package {
import flash.display.Shape;
public class Sides extends Shape {
public function Sides() {
var triangleHeight:uint = 100;
// red triangle, starting at point 0, 0
this.graphics.beginFill(0xFF0000);
this.graphics.moveTo(triangleHeight / 2, 0);
this.graphics.lineTo(triangleHeight, triangleHeight);
this.graphics.lineTo(0, triangleHeight);
this.graphics.lineTo(triangleHeight / 2, 0);
this.graphics.endFill();
trace("Into construct");
}
}
}

AS-3 - How to create different draw styles?

I know the flash.display.Graphics class is final and can't be extended nor its objects in sprites replaced. But I need to change its behavior or fake changed behavior. Normally in OOP I would extend a class, override its methods and call super methods like I want. But since that is not possible with Graphics, what is the best technique to achieve that ?
Is prototyping a way to modify the Graphics class the way I want ?
Let's say I want to draw squares instead of circles:
public class Graphics2 extends Graphics
{
override public function drawCircle(x: NUmber, y: Number, radius: Number): void
{
super.drawRect(x - radius, y - radius, 2 * radius, 2 * radius);
}
}
This is what I wished I could do. How can I fake it ? In the end I want to have objects that represent different draw styles. When I replace my Graphics2 object I just change the complete style of drawing (circles turn to squares etc.)
Why not just map your own class to the methods you can't extend?
public class Graphics2 {
import flash.display.Graphics;
public static function drawCircle(obj:DisplayObject, x:Number, y:Number, r:Number):void {
obj.Graphics.drawRect(x - radius, y - radius, 2 * radius, 2 * radius);
}
}
A non-override-able Class will always remain that, and the classes that extend it will always use that final method, so any change would have to be mapped on to the actual functions (IMHO).

How to pick up a sprite?

A simple question: how could I pick up a sprite in libgdx? By this, I mean that when I click / touch the screen, it checks which (if any) sprite is clicked.
if(Gdx.input.justTouched())
{
cam.unproject(touchPoint.set(Gdx.input.getX(),Gdx.input.getY(), 0));
if(Assets.playButton.getBoundingRectangle().contains(touchPoint.x,touchPoint.y ))
{
// do what u want to do when image is touched
}
Well Assets.playButton is actually a sprite type object
getBoundingRectangle() gives u the rectangle enclosed by the sprite
P.S:- touchPoint is a Vector3 type object