Flopfoot
15 posts
|
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)?
|
SpecificGames
5 posts
|
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);
|
ssjskipp
258 posts
|
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.
|
Jabor
11382 posts
|
You can use BitmapData.copyPixels() to copy part of an image into a new bitmap.
|
ssjskipp
258 posts
|
|
UnknownGuardian
6220 posts
|
Originally posted by ssjskipp:
I beat you to it :D!
AH, but his is better documented.
|
Wordblind
1053 posts
|
Since the last time we had this discussion, I’ve decided I like DisplayObject.scrollRect.
|
ssjskipp
258 posts
|
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.
|
Wordblind
1053 posts
|
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);
}
}
}
|