How to convert Flash game to use BitmapData? - actionscript-3

Currently I have a Flash AS3 game that doesn't use any BitmapData techniques. However, due to performance reasons, I'd like to implement it.
I cannot find a single comprehensive tutorial for using Bitmap and BitmapData classes for a game.
Essentially I have a World class which acts as the game "world" container which holds all game objects (players, items, etc). Within World is the only ENTER_FRAME event which then ticks each object:
private function frameHandler(e:Event):void {
if (!isPaused) {
for (i = 0; i < characters.length; i ++) {
characters[i].tick();
}
for (i = 0; i < controllers.length; i ++) {
controllers[i].tick();
}
//Etc...
}
}
How can I implement BitmapData into this system?
EDIT
Currently all of my world objects are using highly detailed vector graphics which cause performance to suffer when there are many on stage. This is why I'm turning to bitmaps.

Related

Detect which movieclip you hit (AS3)

Im developing a game in AS3, And I ran into problem.
I have the movieclip: ExitPoints. And inside this movieclip, there some other movieclips like: e_1, e_2, e_3 and e_4.
When the player hit the ExitPoints movieclip, I want the game to trace which movieclip the player is standing on (e_1 or e_2 or e_3 or e_4).
I know I can make a code like:
if(player.hitTestObject(ExitPoint.e_1){
}
but I want to do it automaticlly with creating a lot of if statements.
Any help?
This can be accomplished using a loop that loops through all the exits contained in an array.
var exits:Array = [ExitPoint.e_1, ExitPoint.e_2, ExitPoint.e_3, ExitPoint.e_4];
for(var i:int = 0; i < exits.length; i++) {
if(player.hitTestObject(exits[i])) {
//do something
}
}

AS3 tween object not working with .hitTestObject()

I am having a major problem in my new browser app.
Okay so I made game where different cubes (squares) spawn at the top of the screen and I use the Tween class to make them go down the screen and then disappear.
However I want to detect a collision when a cube hits the player (that is also a flying cube).
I tried everything, truly everything but it does not seem to work. The problematic thing is that when I remove the "Tween" function it does detect collision with the hitTestObject method but when I add the "Tween" line collision won't be detected anymore.
It looks like this:
function enemiesTimer (e:TimerEvent):void
{
newEnemy = new Enemy1();
layer2.addChild(newEnemy);
newEnemy.x = Math.random() * 700;
newEnemy.y = 10;
if (enemiesThere == 0)
{
enemiesThere = true;
player.addEventListener(Event.ENTER_FRAME, collisionDetection)
}
var Tween1:Tween = new Tween(newEnemy, "y", null, newEnemy.y, newEnemy.y+distance, movingTime, true);
}
And the collision detection part:
private function collisionDetection (e:Event):void
{
if (player.hitTestObject(newEnemy))
{
trace("aaa");
}
}
I am desperate for some information/help on the topic, it's been bugging me for days.
Thanks for your time, I would be very happy if someone could help me out^^
First, make sure the "newEnemy" instance and the "player" instance are within the same container. If they are not, their coordinate systems might not match up and could be the source of your problem.
Otherwise, you need to keep a reference to each enemy instance you create. It looks like you are only checking against a single "newEnemy" variable which is being overwritten every time you create a new enemy. This might be why you can successfully detect collision between the player and the most recent "enemy" instance.
So... you need a list of the enemies, you can use an Array for that.
private var enemyList:Array = [];
Every time you create an enemy, push it to the Array.
enemyList.push(newEnemy);
In your "collisionDetection" function, you need to loop through all of the enemies and check if the player is touching any of them.
for(var i:int = 0; i < enemyList.length; i++)
{
var enemy = enemies[i];
if (player.hitTestObject(enemy))
{
trace("Collision Detected!");
enemy.parent.removeChild(enemy); // remove the enemy from the stage
enemies.splice(i, 1); // remove the enemy from the list
}
}
I'd suggest that you move to TweenMax, it just might solve your problem, and in my experience it's much better in every possible way.
Scroll down the following page to see a few variations of this library, I myself use TweenNano, they're completely free of charge:
https://greensock.com/gsap-as
I think some plugins cost money, but I doubt you'll ever need them.

Could somebody help me out by telling me how to convert Actionscript 2 to actionscript3?

I devised a simple game that is at mice using actionscript 3. I do not understand how to change the code of actionscript 2 to actionscript 3, please help me.
Could somebody help me out by telling me how to convert Actionscript 2 to actionscript3 ?
jumlah = 0;
waktu_muncul = 0;
function buat_tikus()
{
nomor = random(3)+1;
tikus=attachMovie("tikus","tikus",2{_x:_root["lingkaran"+nomor]._x,_y:_root["lingkaran"+nomor]._y});
tikus.onEnterFrame = function()
{
waktu_muncul++;
if (waktu_muncul == 0)
{
this.gotoAndStop(4);
}
if (waktu_muncul == 20)
{
removeMovieClip(this);
buat_tikus();
waktu_muncul = 2;
}
tikus.onPress = function()
{
sound1 = new Sound();
sound1.attachSound("Suaratikus");
sound1.start(0,1);
var tikus:MovieClip;
if (jumlah == 90)
{
stop();
nextFrame();
}
if (this._currentframe == 1)
{
jumlah += 10;
this.gotoAndPlay(2);
}
};
};
}
onEnterFrame = function ()
{
fps++;
if (fps == 20)
{
timer_txt--;
fps=0
if (timer_txt==0)
{
stop();
gotoAndStop(6);
}
}
palu.swapDepths(_root.getNextHighestDepth());
palu._x = _xmouse;
palu._y = _ymouse;
};
onMouseDown = function ()
{
if (palu._currentframe == 1)
{
palu.gotoAndPlay(2);
}
};
buat_tikus();
var fps : Number = 0;
var timer_txt : Number = 20;
BotMaster's response to this question is right on the money; not to belabor the point, but I have been in this person's shoes, and I can understand the frustration of migrating from AS2 to AS3. Long term: Learn AS3. Short Term: if you have this game, and somehow you need to make it work ASAP (learning AS3 will take some time), one thing you might try is to create an empty AS3 swf that loads your complete game. That might work (but like I said, long term AS3 is more powerful).
Also, depending on how you created the game, you might try the Google Swiffy extension which might convert some basic AS2 games. Note that it WONT convert as2 to as3 but it might make the game accessible on the web. Best of luck
To be clear on AS2/AS3 conversion. This is not possible, those are 2 different languages and their keywords/global methods/properties/event system/display list, etc ... do not match and for that reason are incompatible. That is why for example there's no tool that exist that can convert in one click of a button a AS2 code to a AS3 code.
There are similarities though in those 2 languages and some pieces of AS2 code can sometimes work as is in AS3, other pieces of code can sometimes be converted quite easily, and of course other pieces of code cannot at all be converted and a new AS3 code has to replace the old AS2 code.
All in all only a translation can be done between the two languages and this has to be done line by line. Evaluating if the line is compatible and if it's not replacing it with something that will work in AS3, etc ...
To add to the difficulty some AS2 projects can have code placed inside symbols all over the place. In that case a translation to AS3 will be very unlikely to achieve.

Animations won't start from the beginning when calling gotoAndPlay -- Actionscript 3

I have animations in my second frame ( a falling bird and moving pipes ), once i jump to the third frame i give the user the choice to restart the game by doing gotoAndPlay(1) on a button click, the problem is once i go back the the second frame, the animations continue on playing and not starting from the beginning, i want to restart all the frame animations from their initial positions, what i did is the give every object on the stage the starting (X,Y) coordinates in the script, but it gets difficult when i have so many objects, is there a better way of using this!
First things first: if you're making a game, you probably shouldn't be performing any mission-critical logic on the timeline itself. Look into OOP tutorials.
That being said, what you're looking for is a recursive gotoAndPlay, meaning that you want all the children of the stage and all of their children (etc) to play from the first frame:
function recursiveGotoAndPlay(clip:DisplayObjectContainer, frame:int):void
{
for(var i:int = 0; i < clip.numChildren; i++)
{
var child:DisplayObject = clip.getChildAt(i);
if(child is DisplayObjectContainer)
{
recursiveGotoAndPlay(child as DisplayObjectContainer, frame);
}
}
if(clip is MovieClip)
{
MovieClip(clip).gotoAndPlay(frame);
}
}
Instead of calling the native gotoAndPlay(1), you will call recursiveGotoAndPlay(stage, 1).

AS3 collision with an eventual object

I still did not understand what to do in situations like this...
I'm trying to develop a simple maze game where you can't touch the walls or you'll lose.
I've created a player class and added a child of it using stage.addChild(player) in a Main class.
I have also put a walls object graphically on the stage... When i run the game, it says of course that walls is an undefined property. Then how should I explain this "EVENTUAL" walls presence in the class Player --> this.hitTestObject(XXX)?
You may check collision in your main class
var player:Player;
var walls:Array;
function checkCollistion():void
{
for each (var wall:YourWallClass in walls)
{
//here is the simplest wall to do check hitTestObject
//you can optimize it
if (player.hitTestObject(wall)
{
}
}
}
Or you could keep a walls reference in your player class. So when you create a playe instance with the walls define in the main class.
public class Player
{
private var walls:Array;
public function Player($walls:Array)
{
walls = $walls;
}
}