|
-
*** HOLY ****E! I have a background being drawn!!!! ***
Thanks mr_malee! Thanks Squize! Time for a quick update!!!!
-
This is one of the things I've been battling the last few days. I thought I was going crazy...
A) This line in FlashDevelop does not work:
private var mybackground:BitmapData = new BackgroundImg().BitmapData;
B) but this line does work:
private var mybackground:BitmapData = new BackgroundImg().bitmapData;
The only difference is the capital "B" being changed to lowercase. And to make matters worse, the capital version is recognized/highlighted in FlashDevelop. I hate these kinds of fked up ide problems. ok..back to the update.
-
Developer
You don't need to instantiate the class using the brackets unless the class constructor contains required parameters.
Also FlashDevelop is probably just recognising the class name (BitmapData) and not checking to see whether that property is part of the BitmapData class.
I'm glad you've got it going!!!
-
M.D.
totally understand.
get some cash and invest in FDT or FlexBuilder. Those ide's actually look at the code and spot errors.
still, flashdevelop is 300% better than flash ide
-
Thanks for all of the ongoing help! Very much appreciated. Yep, looks like I'll need to invest in a commercial IDE at some point for sure.
Ok..so now I can:
- Embed images.
- Setup a backbuffer.
- Blit (tiles, sprites, etc) from embedded images onto the backbuffer.
All is very straight forward now and easy to comprehend. 
The next step for me is to create a basic custom class that will define each sprite so I can initiate a bunch of them and do some performance testing. 
Something like this:
Code:
public class Csprite
{
private var x:Number;
private var y:Number;
private var img:BitmapData;
private var srcx:int;
private var srcy:int;
private var width:int;
private var height:int;
}
I don't know where in my project I put the new class definition? Do I include it in my main.as or create a new Csprite.as file? Then how would I access this new class in my main.as file so I can do something like:
private var sp:Csprite = new Csprite;
Thanks!
--------------------------------------
Here's the current code that actually blits from a sprite image onto a background. It's nothing pretty but it is the foundation for greater things to happen. 
Code:
package
{
import flash.display.*;
import flash.events.*;
import flash.utils.*;
import flash.geom.*;
import flash.events.*;
//
//
//
public class Main extends Sprite
{
//
// Embed my background image
//
[Embed(source = "background.jpg")]
private var BackgroundImg:Class;
[Embed(source = "sprites.png")]
private var SpritesImg:Class;
private var mybackground:BitmapData = new BackgroundImg().bitmapData;
private var mysprites:BitmapData = new SpritesImg().bitmapData;
//
private var backBufferData:BitmapData; //backbuffer bitmap data to draw on
private var backBuffer:Bitmap; //the bitmap object
private var point:Point;
private var rect:Rectangle;
//
private var xpos:Number;
private var ypos:Number;
private var xposdir:Number;
private var yposdir:Number;
//
//
//
public function Main()
{
backBufferData = new BitmapData(512,512 , false , 0x666666); //data storage of the backbuffer
backBuffer = new Bitmap(backBufferData); // "canvas" of the backbuffer
addChild(backBuffer); //add the backBuffer canvas object to the displaylist of the stage
//
xpos = 0;
ypos = 0;
xposdir = 1;
yposdir = 1;
//
addEventListener(Event.ENTER_FRAME, Mainloop);
}
//----------------------------------------------------------------------------
// mainloop
//----------------------------------------------------------------------------
private function Mainloop(e:Event):void
{
Update();
Render();
}
//----------------------------------------------------------------------------
// update objects
//----------------------------------------------------------------------------
private function Update():void
{
//
// Update all game objects here
//
xpos = xpos + xposdir;
ypos = ypos + yposdir;
if (xpos<0 || xpos>511)
{
xposdir = -xposdir;
xpos = xpos + xposdir;
}
if (ypos<0 || ypos>511)
{
yposdir = -yposdir;
ypos = ypos + yposdir;
}
}
//----------------------------------------------------------------------------
// render objects
//----------------------------------------------------------------------------
private function Render():void
{
backBufferData.lock();
//
// Render all objects here
Blit(mybackground,0,0,0,0,512,512)
Blit(mysprites,xpos,ypos,0,0,32,32)
//
backBufferData.unlock();
}
//----------------------------------------------------------------------------
// Blit Function
//----------------------------------------------------------------------------
private function Blit(image:BitmapData , x:Number , y:Number , sx:int , sy:int , w:int , h:int ):void
{
point = new Point(x,y);
rect = new Rectangle(sx,sy,w,h);
backBufferData.copyPixels(image , rect , point ,null , null , true);
}
}
}
-
Hype over content...
"I don't know where in my project I put the new class definition?"
Mate, please take a look at that source that I linked to. I didn't do it for ego purposes, it's a complete game in as3 with a class structure in place.
There will be loads of things for you to take away from it to make your life easier ( But to answer a bit of your question, yes you would use a class just for it. I normally have a Handler class which manages all these instances from one place, which covers the init, mainloop and housekeeping of all of them ).
"You don't need to instantiate the class using the brackets unless the class constructor contains required parameters."
Really ? I never knew that. It's not something I'll be doing though, it's one of those shorthand things that look so wrong to me ( I still always have to put ==true in conditionals rather than skip it. Coders OCD I guess ).
Squize.
-
"Mate, please take a look at that source that I linked to." Yep..that did the trick. I wasn't putting "package" around my other .as files. Thank you...very much!
well... I now have the sprite demo working. Actually, I'm fairly impressed by the performance. I'm getting 2000 sprites at 30fps. Which is very comparible to my Blitzmax hardware gpu driven engine. Ofcourse this is using CopyPixels() which has no fx on the blit.
Here's my copypixels blit function:
Code:
private function Blit(image:BitmapData , x:Number , y:Number , sx:int , sy:int , w:int , h:int ):void
{
point = new Point(x,y);
rect = new Rectangle(sx,sy,w,h);
backBufferData.copyPixels(image , rect , point , null , null , true);
}
I'd like to experiment with using Draw() instead of copyPixels. Can someone tell me how to convert the above function to Draw() instead? I tried google searching on "AS3 .Draw" etc, but couldn't find anything exact. Thanks!
-
Also, I'm able to read MouseX,MouseY fine, but how do I tell if the mouse has been clicked? I tried this:
addEventListener(MouseEvent.CLICK, MouseClicked);
But MouseClicked is never getting called. Is there a way to read it on the fly, something like If (MouseDown) etc, etc? Thanks.
-
Custom User Title
Draw is slower, the only reason to use it is if you need rotation or fancy color change with ColorTransform
And the MouseClick, i gess thats a crude solution but i think that by putting one big white MovieClip in the background it will be clickable
-
Thanks Incrue, I still can't get any mouse clicks to be recognized. Even if my class Main Extends Sprite or MovieClip, the mouse click doesn't register.
I'll start another thread for more specific questions. 
I think this thread has gone as far as it can for now. I have specific questions related to AS3 game coding with copypixels, draw, etc, and I think I'll make separate questions for those. Thanks again for all the good info, links, etc. 
Code:
package
{
//
// Basic Imports
//
import flash.display.*;
import flash.events.*;
import flash.utils.*;
import flash.geom.*;
import flash.events.*;
//
// Custom Imports
//
import classes.*;
import de.polygonal.ds.DLinkedList;
import de.polygonal.ds.DListIterator;
import de.polygonal.ds.DListNode;
//
//
//
public class Main extends MovieClip
{
//
// Embedded Graphics
//
[Embed(source = "background.jpg")]
private var BackgroundImg:Class;
[Embed(source = "sprites.png")]
private var SpritesImg:Class;
//
// Setup array to hold all of the graphics for easy access
//
private var img0:BitmapData = new BackgroundImg().bitmapData;
private var img1:BitmapData = new SpritesImg().bitmapData;
private var myImages:Array = new Array(img0, img1);
//
// Now we can access our graphics by: myImages[0], myImages[1], etc.
//
private var backBufferData:BitmapData; //backbuffer bitmap data to draw on
private var backBuffer:Bitmap; //the bitmap object
private var point:Point;
private var rect:Rectangle;
//
// Misc Vars
//
private var i:int; // for loops
private var spritelist:DLinkedList; // Linked List
//
//
//
public function Main()
{
backBufferData = new BitmapData(640,640 , false , 0x666666); //data storage of the backbuffer
backBuffer = new Bitmap(backBufferData); // "canvas" of the backbuffer
addChild(backBuffer); //add the backBuffer canvas object to the displaylist of the stage
//
// Put 100 sprites on the screen
//
spritelist = new DLinkedList;
for (i=0; i<100; i++)
{
AddASprite();
}
//
//
//
addEventListener(Event.ENTER_FRAME, Mainloop);
addEventListener(MouseEvent.CLICK, MouseClicked);
}
//
//
//
private function AddASprite():void
{
var sp:Csprite = new Csprite;
sp.x = (Math.random() * 512);
sp.y = (Math.random() * 512);
sp.xdir = ((Math.random() * 10) + 1) * .5;
sp.ydir = ((Math.random() * 10) + 1) * .5;
sp.srcx = (Math.random() * 7);
sp.srcx = sp.srcx * 48;
sp.srcy = 0;
sp.img = 1;
sp.width = 48;
sp.height = 48;
spritelist.append(sp);
}
//
//
//
private function MouseClicked(e:MouseEvent):void
{
AddASprite();
}
//
// mainloop
//
private function Mainloop(e:Event):void
{
Update();
Render();
}
//
// Update All Objects
//
private function Update():void
{
//
// Sprites
//
var item:DListNode = spritelist.head;
while (item)
{
item.data.update();
item = item.next;
}
}
//
// Render All Objects
//
private function Render():void
{
backBufferData.lock();
//
// Main background
//
Blit(myImages[0], 0, 0, 0, 0, 640, 640);
//
// Sprites
//
var item:DListNode = spritelist.head;
while (item)
{
Blit(myImages[item.data.img], item.data.x - item.data.width/2, item.data.y - item.data.height/2, item.data.srcx, item.data.srcy, item.data.width, item.data.height);
item = item.next;
}
//
// For the hey of it, blit a sprite where the mouse is
//
Blit(myImages[1], mouseX-24, mouseY-24, 0, 0, 48, 48);
//
//
//
backBufferData.unlock();
}
//----------------------------------------------------------------------------
// Blit Function
//----------------------------------------------------------------------------
private function Blit(image:BitmapData , x:Number , y:Number , sx:int , sy:int , w:int , h:int ):void
{
point = new Point(x,y);
rect = new Rectangle(sx,sy,w,h);
backBufferData.copyPixels(image , rect , point , null , null,true);
}
}
}
-
M.D.
read read read: http://help.adobe.com/en_US/ActionSc...mmingAS3_Flex/
as to your mouseEvent problems, you need to add those to the stage.
stage.addEventListener(...
-
Hey!! All I had to do was add "stage." to my addlistener line and mouse clickys working. woo hoo! Thanks Mr.Malee, and that's a good read in that link as well.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|