How can I draw parts of an image or load them into an array?

Subscribe to How can I draw parts of an image or load them into an array? 9 posts

Sign in to reply


 
avatar for Flopfoot Flopfoot 15 posts
Flag Post

Say if I have this picture of a deck of cards
http://math.hws.edu/javanotes/c12/cards.png

And at any point on my screen I only want to draw one card, say the seven of diamonds. How can I do this?

Is there an easy way to load all 55 of these images into an array of images (even though I’ve only got 1 file)?

 
avatar for SpecificGames SpecificGames 5 posts
Flag Post

I’ll try using a mask. Create 55 frames in one MovieClip. Then place the mask in each seperate place. Then you can acess each card by typing

a = //frame you want gotoAndStop(a);
 
avatar for ssjskipp ssjskipp 258 posts
Flag Post

Use bitmap data.
use copyPixels function to copy a rectangular area of pixels from the source image. Use maths to figure out which card’s rectangle is which.

 
avatar for Jabor Jabor 11382 posts
Flag Post

You can use BitmapData.copyPixels() to copy part of an image into a new bitmap.

 
avatar for ssjskipp ssjskipp 258 posts
Flag Post

I beat you to it :D!

 
avatar for UnknownGuardian UnknownGuardian 6220 posts
Flag Post
Originally posted by ssjskipp:

I beat you to it :D!

AH, but his is better documented.

 
avatar for Wordblind Wordblind 1053 posts
Flag Post

Since the last time we had this discussion, I’ve decided I like DisplayObject.scrollRect.

 
avatar for ssjskipp ssjskipp 258 posts
Flag Post
Originally posted by UnknownGuardian:
Originally posted by ssjskipp:

I beat you to it :D!

AH, but his is better documented.

True that. But gives him a place to start with usage, whereas someone who doesn’t know how to read API would get lost and just post again with “HOW DOWS I COPY PIXELS!?”

And Wordblind, that would require each card to have the ENTIRE bitmap inside it, not just the needed pixels.

 
avatar for Wordblind Wordblind 1053 posts
Flag Post
And Wordblind, that would require each card to have the ENTIRE bitmap inside it, not just the needed pixels.

Yes, but every Bitmap could point to the same BitmapData, so it would actually take up less memory than making copies of the individual portions.

EDIT: Like this

package  {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Rectangle;

	public class Card extends Bitmap {
		[Embed(source = "cards.png")] private static var Deck:Class;
		private static var deckBmp:BitmapData = (new Deck()).bitmapData;
		private static var cardW:uint = deckBmp.width / 13, cardH:uint = deckBmp.height / 5;
		private static var backRect:Rectangle = cardRect(2, 4);
		private var frontRect:Rectangle;
		
		public static const CLUBS:uint = 0, DIAMONDS:uint = 1, HEARTS:uint = 2, SPADES:uint = 3;
		
		public function Card(suit:uint, rank:uint, showFront: Boolean = true) {
			super(deckBmp);
			frontRect = cardRect(suit, rank - 1);
			scrollRect = showFront ? frontRect : backRect;
		}
		
		public function showFront():void { scrollRect = frontRect; }
		public function showBack():void { scrollRect = backRect; }
		public function flip():void { scrollRect = (scrollRect == frontRect) ? backRect : frontRect; }
		
		private static function cardRect(row:uint, col:uint):Rectangle {
			return new Rectangle(row * cardW, col * cardH, cardW, cardH);
		}
	}
}

Sign in to reply