[AS3(FD)]Importing Bitmap question [Solved]

23 posts

Flag Post

I have no idea how to start this; I can’t figure out how to draw complex images with code yet so I want to draw my objects in paint and import the bitmaps through code. How would I go about doing this?
Would I use graphics.beginBitmapFill?

Also, is it recommended I have a separate function for drawing/importing all the objects?

Another question: I’ve heard talk about blitting, what is it and is it relevant to what I am talking about?

Code For Future Reference:

package Uzzwars
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.Bitmap;
	/**
	 * ...
	 * @author uzzbuzz
	 * @subauthor bobwashere (Looks like Bob Washer)
	 */
	public class Main extends Sprite {
	
	[Embed(source = "../../lib/player1.png")]
	private var player1:Class; 
	public function Main()
	{
            var image:Bitmap = new player1();
            addChild(image);
        }
   }
}
 
Flag Post

[Embed(source = ‘/image3.png’)]
private var image3 : Class;
var image3bm:Bitmap = new image3();

The use image3bm as a normal bitmap.

blitting is essentially drawing out all your images you might want at the start of the game (or embedding them), and using copypixels to copy the right ones to the game screen each time it changes.

 
Flag Post

I get a couple errors with that:

col: 10 Error: Syntax error: expecting rightparen before var. private var player1 : Class;

col: 2 Error: Syntax error: expecting rightbracket before var. var player1bm:Bitmap = new player1();

col: 22 Error: Syntax error.[Embed(source = ‘/player1.png’)]

col: 21 Error: Syntax error.[Embed(source = ‘/player1.png’)]

 
Flag Post

Do you have a player1.png in the same folder as your code?

 
Flag Post

Sort of, they are in different subfolders.

The code is in /Space Shooter/src/Uzzwars
and player1.png is in /Space Shooter/lib

I thought that wouldn’t be a problem because the project has the root folder /Space Shooter

 
Flag Post

The (source = ‘/image3.png’) part is relative. just a / assumes the image is in the same folder, you’ll have to change it to whatever the path to that png is….‘C:/something/something/Space Shooter/lib/player1.png’ probably.

 
Flag Post

It doesn’t seem to work with [Embed(source = ‘G:\Xtra\Games\Space Shooter\Space Shooter\lib\player1.png’)]

edit: I did have that, I just didn’t copy it.

 
Flag Post

.png

 
Flag Post

This could be just a formatting issue on these forums but… are you using ’ ? :P
I forget the name of them, but anyway the single quote ( ’ ) should work, your posts are using tick marks ` which caused the same syyntax error you’re seeing when I tested it..

edit- I see that mine have been converted, so it probably is a formatting thing. So if you happened to copy & paste the code you need to replace the ‘and’ with single quotes

 
Flag Post
  1. Add the image to the lib folder
  2. Right-click it and choose ‘Generate Embed Code’
  3. Beneath the line that appears (this should be under public class Wahtever extends... but before public Whatever()), add private const MyImg:Class;
  4. Now, in your code you can do stuff like: var myImg:Bitmap = new MyImg(); addChild(myImg);
 
Flag Post
Originally posted by uzzbuzz:

Another question: I’ve heard talk about blitting, what is it and is it relevant to what I am talking about?

these should answer the question

article:
http://www.adobe.com/devnet/flash/articles/blitting_mc.html

video (3 parts):
http://gotoandlearn.com/play.php?id=140
http://gotoandlearn.com/play.php?id=141
http://gotoandlearn.com/play.php?id=142

 
Flag Post
Originally posted by BobTheCoolGuy:

  1. Add the image to the lib folder
  2. Right-click it and choose ‘Generate Embed Code’
  3. Beneath the line that appears (this should be under public class Wahtever extends... but before public Whatever()), add private const MyImg:Class;
  4. Now, in your code you can do stuff like: var myImg:Bitmap = new MyImg(); addChild(myImg);

This worked great, but I still have an error with the next line:

private var player1:Class;  

Error #1013: The private attribute may be used only on class property definitions.

 
Flag Post

Put the embed tags inside a class! Can’t just have them lying around in a package.

 
Flag Post

I’m still having trouble, and all I want so far is for the .png to appear when I ctrl-enter. I updated the op to show my code so far. I’m getting the same error as my prior post. The embed is within the Main class.

 
Flag Post

You are using FD, correct?
Also, please post your newest code.

 
Flag Post

I am using FD and the newest code is in the OP.

 
Flag Post

Try this:

package Uzzwars
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.Bitmap;
	/**
	 * ...
	 * @author uzzbuzz
	 * @subauthor bobwashere
	 */
	public class Main extends Sprite {
	
	[Embed(source = "../../lib/player1.png")]
	private var player1:Class; 
	public function Main()
        {
            var image:Bitmap = new player1();
            addChild(image);
        }
   }
}
 
Flag Post

I get Error #1084: Syntax error: expecting rightbrace before leftbrace. for the line under Main()
and Error #1071: Syntax error: expected a definition keyword (such as function) after attribute public, not Main. for the line Main()

 
Flag Post

Embeds need to be at the class level, not inside a function.

Here’s an example from some of my code:

package RedCorona.BoxPlatformer.LevelEntity.View {
	import RedCorona.BoxPlatformer.LevelEntity.Exit;
	import RedCorona.Drawing.IAnimationSource;
	import flash.display.Bitmap;
	import RedCorona.Platform.LevelEntity.View.EntityView;

	public class ExitView extends EntityView implements IAnimationSource {


		// Image part
		[Embed(source = '../../../../../resource/img/exit.png')]
		private static var ImageClass:Class;
		private static var image:Bitmap = new ImageClass();


		override public function get Image() : Bitmap { return image; }		
		public function get FrameWidth() : int { return 32; }
		public function GetFrameLength(frame:int): int { return 10; }
		
		public function ExitView(target:Exit) {
			super(target);
		}
		
	}
	
}

You can ignore everything except the ‘Image part’. A minimal implementation would be

public class ImageSource {
 [Embed(source = '../lib/mypic.png')]
 private static var ImageClass:Class;
 
 public function get image() : Bitmap {
  var image:Bitmap = new ImageClass();
  // do stuff with the image if you want
  return image;
 }
}

You can then use imageSource.image however you like (e.g. using addChild to get it into the display tree).

Note the level at which the embed is defined (at the class level). You can make it static because by definition there can only be one source that is being used. You can usually make the image a static field as well, but if you want to do different things with the Bitmap you might need to create that at the instance level as in the second example.

 
Flag Post
Originally posted by uzzbuzz:

I get Error #1084: Syntax error: expecting rightbrace before leftbrace. for the line under Main()
and Error #1071: Syntax error: expected a definition keyword (such as function) after attribute public, not Main. for the line Main()

The code should be syntactically sound. Maybe you copied and pasted it wrong?

 
Flag Post

:S All I had to do from bob’s answer was to add function. I have a question just for knowledge, since it works now: why is it that the image shows up now but it doesn’t if I change the function name to something else (eg draw)

If I change it, it shows painful yellow text saying Warning: return value for function ‘draw’ has no type declaration.

Thanks for the help! :)

 
Flag Post
Originally posted by uzzbuzz:

:S All I had to do from bob’s answer was to add function. I have a question just for knowledge, since it works now: why is it that the image shows up now but it doesn’t if I change the function name to something else (eg draw)

If I change it, it shows painful yellow text saying Warning: return value for function ‘draw’ has no type declaration.

Oh my fault sorry. The reason is, when you have a function with the same name as the class, it’s a special function called the constructor. This is called whenever you do new ClassName(). For the Main class, this happens automatically when the swf starts. Constructors never have return types. On the other hands, regular functions are only called when you call the function. For this, you must specify a return type. If you’re not returning anything it’s void, so, something like this: public function myFunction():void.

 
Flag Post

Bob’s (other Bob’s) code looks okay, I wonder if you had some stray braces in the file you tried to paste into.

Main doesn’t need a type declaration because it’s a constructor; the return type of a constructor is implicitly the type it’s declared in, so you don’t need to say so. (That is, the return type of ‘Main’ is ‘Main’.) If you change its name (or change the class name), it’s now a normal function, and those do need return type declarations – in this case, void – and you would then have to explicitly call the method as well as calling new Main().

edit: lol sniped