ActionScript 3 not showing jpg - actionscript-3

Im working with Starling and im trying to show a jpg on my background, but it doesnt shows up. Here is the embed code:
[Embed(source="../media/img/backgroundmenu.jpg")]
public static const BgWelcome:Class;
Later on I cast it into a bitmap and make it a texture. Im getting the following error at the getter of the bitmap:
ReferenceError: Error #1069: Property metaData not found on resources.Assets_BgWelcome and there is no default value.
The jpg is at the exact folder, tried to place it with the absolute path, but nothing apears.
Thanks in advance.

Here's a nice way to go about creating a convenient container for all your graphics using starling:
public class Art {
[Embed(source = "../textures/foo.jpg")] private static const FooBitmap:Class;
public static var FooTexture:Texture = MakeTexture(FooBitmap);
private static function MakeTexture(_Texture:Class):Texture {
var bitmap:Bitmap = new _Texture();
return Texture.fromBitmap(bitmap);
}
}
Usage:
var foo:Image = new Image(Art.FooTexture);

Related

Changing world's in flashpunk

I'm trying to make my first actual game in AS3, for this I am using flashpunk due to it's simplicity.
I have got to the point where I have a moving character and a textbox when I go infront of a door.
When I am infront of a door, two variables are changed inside the player class:
public var onDoor:Boolean = false;
public var doorType:String = ""
You can probably guess what onDoor does, doorType is the name the class. It's set like this:
public static var BedroomDoor:Door = new Door(350, 331, "ApartmentBedroom")
ApartmenBedroom being the name of the world the door leads to.
Inside the main class where flashpunk is initialised, I have this function:
public static function ChangeLevel(world:String)
{
var newWorld = getDefinitionByName(world) as Class
FP.world = new newWorld
}
But when I go to the door and press X infront of the door which calls the function, I always get this error:
[Fault] exception, information=ReferenceError: Error #1065: Variable ApartmentBedroom is not defined.
Can anyone help fix this?
In order to use getDefinitionByName() you have to include your class in the code first. Anything will do, for example simple var a:ApartmentBedroom; or more complex one like var allLevelsClasses:Array = [ApartmentBedroom, ApartmentLivingRoom];
So your Main.as code should look like this:
public static function ChangeLevel(world:String)
{
var allLevelsClasses:Array = [ApartmentBedroom, ApartmentLivingRoom]; // put everything here
var newWorld = getDefinitionByName(world) as Class
FP.world = new newWorld
}
Also you could avoid full classname confusion when creating Door. Instead of passing string with its name into Door constructor just pass world's class:
private var worldClassName:String;
public function Door(x:Number, y:Number, worldClass:Class):void
{
/// init what's necessary
this.worldClassName = getQualifiedClassName(worldClass);
}
And later just:
public static var BedroomDoor:Door = new Door(350, 331, ApartmentBedroom)

GetDefinition in as3 using sprite class

i have a problem. I have been able to run my game successfully using as3isolib library (IsoSprites), but when i search on the internet, the IsoSprites don't have the hitTestObject or hitTextPoint features. So, i changed the IsoSprites to the Sprite, but when i already changed the IsoSprites to the Sprite, i am getting this error:
1119: Access of possibly undefined property sprites through a
reference with static type flash.display:Sprite.
and it is pointed in:
Constant.dudeEfis.sprites = [efisFrontClass];
dudeEfis is Sprite
i am aware that the Sprite don't have the feature of:
dudeEfis.sprites
but the problem is, if i change the "dudeEfis" to the IsoSprite, i am not be able to get the hitTestObject or hitTestPoint. And if i change to the Sprite, i am getting that error. How do i solve it? The code that i am giving to you is for movement faces direction (when the player is facing south, so do the character)
Here is the code:
public static var loaderEfis:Loader;
public static var dudeEfis:Sprite; //public static var dudeEfis:IsoSprite;
public static var _numXEfis:Number = 0;
public static var _numYEfis:Number = 0;
Constant.loaderEfis = new Loader();
Constant.loaderEfis.load(new URLRequest("efis.swf"));
var efisFrontClass:Class = Constant.loaderEfis.contentLoaderInfo.applicationDomain.getDefinition("EfisFront") as Class;
if (Constant._numXEfis == 0 && Constant._numYEfis == 0)
{
Constant._numXEfis = Constant.dudeEfis.x;
Constant._numYEfis = Constant.dudeEfis.y;
Constant.dudeEfis.sprites = [efisFrontClass];
}
Any help will be appreciated! Thank you in advance!
Sounds like you already know what the problem is. One way you can solve this is to create your own class that extends the IsoSprite class then add the hitTestObject and hitTestPoint methods yourself, they're pretty easy to implement.
package {
import flash.display.DisplayObject;
import flash.geom.Point;
public class MyOwnSprite extends IsoSprite {
public function hitTestObject (object:DisplayObject):Boolean {
//some logic here
}
public function hitTestPoint (point:Point):Boolean {
//some logic here
}
}
}
This way you get to keep all the functionality of IsoSprite while having those two methods that you need.

Upload Photo in Flex, set the URLRequest

In my Flex Application doing Image upload for local system.
Actually i'm using FileReference class in this class have load() it is only in FP-10.
so i'm doing another way using upload() but this method taking URLRequest object.
private const FILE_UPLOAD_URL:String
fileRef.upload(new URLRequest(FILE_UPLOAD_URL));
so problem is how to get file url in local system..
give me example for this help lot..
Try this:
public class Uploader{
private var fileReference:FileReference;
private static const FILE_UPLOAD_URL:String = "http://somewebsite.com/images/..."
public function Uploader()
{
fileReference = new FileReference();
fileReference.addEventListener(Event.SELECT, onFileSelect);
}
//Call this method to start browsing on local filesystem
private function doUpload():void
{
fileReference.browse();
}
//After selecting a file you can now upload it
private function onFileSelect():void
{
fileReference.upload(new URLRequest(FILE_UPLOAD_URL));
}
}
A full, working example can be found here
Also make sure to check out the Livedocs on this matter
Cheers

AS3 Starling Framework Texture Atlas TypeError #1007

I'm new to the starling framework and am currently learning how to use it.
I've created textures from embedded PNG files with the starling framework that work perfectly and display on the screen, but I am trying to get a spritesheet (Texture Atlas) to work and it is giving me this:
"Error #1007: Instantiation attempted on a non-constructor."
From all of the research I have done the code I have should work.
Here is the applicable code from my Assets class.
public class Assets
{
[Embed(source="assets/sky.png")]
private static var SKY_CLASS:Class;
public static var SKY:Texture;
[embed(source="assets/generalsheet.png")]
private static var GENERAL_SHEET_CLASS:Class;
[embed(source="assets/generalsheet.xml", mimeType="application/octet-stream")]
private static var GENERAL_ATLAS_CLASS:Class;
public static var GENERAL_SHEET:TextureAtlas;
public static function init():void
{
SKY = Texture.fromBitmap(new SKY_CLASS());
GENERAL_SHEET = new TextureAtlas(Texture.fromBitmap(new GENERAL_SHEET_CLASS()), XML(new GENERAL_ATLAS_CLASS())); // this is where Flash Builder tells me there is an error
}
You just need to write the Embed tag in upper case, just change:
[embed(source="assets/generalsheet.png")]
private static var GENERAL_SHEET_CLASS:Class;
[embed(source="assets/generalsheet.xml", mimeType="application/octet-stream")]
private static var GENERAL_ATLAS_CLASS:Class;
to:
[Embed(source="assets/generalsheet.png")]
private static var GENERAL_SHEET_CLASS:Class;
[Embed(source="assets/generalsheet.xml", mimeType="application/octet-stream")]
private static var GENERAL_ATLAS_CLASS:Class;
On a side note, class names are usually written in UpperCamelCase, and ALL_CAPITALIZED is reserved for constants. Variable names are usually written in lowerCamelCase or lowercase_separated_by_underscore. This is a convention followed by most ActionScript3 (and Java) programmers and if you stick to it your code will be more readable and thus it should be easier to help you next time ;)
So I recommend:
[Embed(source="assets/sky.png")]
private static var SkyClass:Class;
public static var sky:Texture;
[Embed(source="assets/generalsheet.png")]
private static var GeneralSheetClass:Class;
[Embed(source="assets/generalsheet.xml", mimeType="application/octet-stream")]
private static var GeneralAtlasClass:Class;
public static var general_sheet;
public static function init():void
{
sky = Texture.fromBitmap(new SkyClass());
general_sheet = new TextureAtlas(Texture.fromBitmap(new GeneralSheetClass()), XML(new GeneralAtlasClass()));
}

Is it possible to create dynamic embed function?

Is it possible to create dynamic embed function in ActionScript3
for example like this
public function embedImage(path:String):Bitmap{
[Embed(source = path, mimeType = "image/png")]
var NewBitmapClass:Class;
var image:Bitmap=new NewBitmapClass();
return image;
}// tried it, it doesnt work
or maybe in some other way, or even if it is at all possible?
The closest you can get with the "dynamic" part, is to create a wrapper class, where you define your images, and you can get them as Bitmap later on by an id.
Unfortunately the properties are public, otherwise the hasOwnProperty function doesn't return true. (If someone finds a better way, please let me know)
See below:
package {
import flash.display.Bitmap;
public class DynamicEmbed {
[Embed(source = "../images/cat.jpg")]
public var cat : Class;
[Embed(source = "../images/parrot.jpg")]
public var parrot : Class;
[Embed(source = "../images/pig.jpg")]
public var pig : Class;
[Embed(source = "../images/quail.jpg")]
public var quail : Class;
public function DynamicEmbed() {
}
public function getBitmap(id : String) : Bitmap {
if(hasOwnProperty(id)) {
var bitmap : Bitmap = new this[id]();
return bitmap;
}
return null;
}
}
}
Embedded elements are embedded at compile time. You can't dynamically embed something at compile time... If you want to load resources dynamically, use the Loader.
No, embed source is embedded at compile time. You can not embed anything at run time. That's what embed means, embedding during building the swf.