Be a Supporter!

ZanzlanzZanzlanz

Main News Movies Games Audio Art Favorites Reviews Stats 1,221 Fans
Follow Zanzlanz

Zanzlanz's News

Porting AS2 to AS3

2014-10-27 11:31:58 by Zanzlanz
Updated

Hi! So for the last few months I've been working intensely on the ActionScript 3 port of Mine Blocks. I've converted tens of thousands of lines of code from AS2 to AS3, and in this process I've learned A TON about how AS3 works compared to AS2. I'm actually quite familiar with AS3, but porting from a MovieClip-based game was a totally different monster for me.

This post is mainly to describe the main differences, issues, and successes I've encountered when porting from ActionScript 2 to ActionScript 3.

Disclaimer: This assumes MovieClips and Spaghetti-coder ;)

 

 

THE BASICS


When porting, there are 3 stages of errors to address as you go through the game.

There are two passes by the compiler. One of the stages lists a massive amount of errors. The other stage lists only a couple errors at a time, and it can be irritating to continuously fix-build-fix-build-fix. Then there are the run-time errors. Some will break the game, others will throw a ridiculous amount of debug errors. There's a point where run time errors are just seen as simple bugs. That's when the main pain is over, and all is left is bug fixing as usual. xD


AS3 is clearly much, much faster.

The world generation of Mine Blocks in AS2 takes about 30 seconds, while in AS3 it only takes under 2 seconds! Incredible! I've read that AS3 was 10 times faster than AS2, but I thought that was an exaggeration! It's really not. Apart from the lag generated by the debugging output, I haven't really noticed any control latency or lag, even with such a complicated game as Mine Blocks.

This is a pretty good reason to port a game from AS2 to AS3.


No code can be written on an object.

Code can be on the object's timeline, or their parent's timeline, but not the object itself. Usually it's pretty easy to port this though - simply convert the onClipEvent to an event handler on the parent timeline. Key and mouse listeners are the same story. They're found in the KeyboardEvent and MouseEvent listener classes. :)


Going from attachMovie to addChild requires the name property to be passed to the new object.

So dropped items in Mine Blocks checked to see what their name was so they could recieve their item type, count, enchantments, etc. The only catch was that in attachMovie, you supplied the clip with an instance name, which set the "name" property. with addChild, you need to give the MovieClip a reference in a variable (most likely a tmp one), and then using that reference, set its name. Pretty simple. Causes headaches though.


Functions (and all other variables) can only be instantiated once.

In AS2, you'd "accidentally" write:

var a = 5;
var a = 5;

and no one would have a fit. But in AS3, the compiler gets a bit angry! Well the same goes for functions. In Mine Blocks, one MovieClip handled all of the different types of blocks, all on different frames. Some blocks had special properties, like flowing water, falling sand, grass checking for sunlight, healing crystals healing, and even leaves leafing. To fix the multiple instantiations, frame one has

var behaviorFunction:Function;
and any frame using a new function will use an anonymous function reference:
behaviorFunction = new function() { /* stuff */ };

It's such a simple fix, but it was a lot of work to change everything. Porting isn't easy!

 

TRICKY ISSUES


AS3 handles events differently.

For better or for worse, they're different. In ActionScript 3.0, they continue to run even when the instance of the MovieClips is removed from the display list. If you have a clip with an ENTER_FRAME event, then go to a different frame that doesn't contain that clip, the ENTER_FRAME event still runs. Additionally, if the clip is added to the display list again, the event will be running multiple times at once! This is due to how the garbage collector works in Flash. Do a search for articles about Flash's garbage collection to get a good idea how and why this happens - it's really helpful to know. Anyway, I had to come up with a few hackish ways to get past some of the issues created by this behavior.

The error you'll usually get from this:
"TypeError: Error #1009: Cannot access a property or method of a null object reference." wherever there's a reference to a different clip (including MovieClip(root) ). Basically, the MovieClip doesn't really exist, so its properties are all null, including its reference to root.

One way to fix this issue is to listen for when the MovieClip is removed. When it is, remove all listeners.

addEventListener(Event.REMOVED_FROM_STAGE, cleanupListeners);
function cleanupListeners(e:Event) {
    removeEventListener(Event.ENTER_FRAME, ef);
    removeEventListener(Event.REMOVED_FROM_STAGE, cleanupListeners);
}

A similar solution that fixes this issue is by starting the ENTER_FRAME event function with:

if(MovieClip(root) == null) return;

This hides the bigger issues though, so if you're using this post as help for your own project, only use this if you're absolutely certain this is the solution for your situation.


Changing frames instantly runs code.

This can cause a stack overflow (try putting gotoAndStop(2); on frame one, and gotoAndStop(1); on frame 2). A problem with this is that if you attach a MovieClip and then change its frame other than frame 1, the initialization code wont run, creating errors. Just be aware. This error will be a give away:
Error #1023: Stack overflow occurred.


Sending GET and POST is fairly different in AS3.

I'm not a professional at how to send and recieve from a server, so I shouldn't post the code to port, here. But there are some resources online to help with porting to it. If you guys really want, I'll write the code. :)

 

QUICK LIST OF DIFFERENCES


1) random(n) was removed. Here's a replacement for the random function for you (just make sure you reference it properly, through MovieClip(root) or something):

function random(n) {
    return(Math.random()*n>>0);
}

2) new Sound(x) was changed. setVolume and setPan were moved to the soundTransform class, which can be applied to the soundChannel class. If I remember correctly, only 32 sound channels can play at once.

3) _x, _y, _width, _height, and _rotation all lost their underscores in AS3.
4) _root is MovieClip(root) in AS3.
5) _xscale and _yscale in AS3 are scaleX and scaleY. Also they are out of 1, not 100.
6) _xmouse and _ymouse have changed to mouseX and mouseY.

7) onEnterFrame = function{ ... } is an event in AS3. It would look something like this:
addEventListener(e:Event, someFunction);
function(e:Event) { ... }

8) hitTest(...) was replaced in AS3 with hitTestPoint() and hitTestObject(). I believe it's the same thing, just split into two functions.

9) int was added to AS3! The Number class is a floating point decimal, still. The int class is an integer.

10) When referencing to a property of an undefined element of an object/array in AS3, it will throw an error. For comparison, in AS2 it would be undefined.

11) swapDepths(...) in AS3 is now achieved through setChildIndex(...). Similarly, getNextHighestDepth() can be achieved through numChildren().

nn) Soooo much more.

 

CONCLUSION


There are a lot of interesting differences to be addressed when porting from AS2 and AS3. It can be a pain to convert, but there's always Find/Replace to help out. Some times it can be useful to comment out code when porting, so you can focus on certain errors first.

If your game or project is fairly big, I do recommend porting it to AS3. It's possible. If the project is as big as Mine Blocks, you need to take in account that it'll be about half of a year of work... but if that means you can benefit from the new features of AS3, then go for it! :D

If you have any questions related to porting AS2 to AS3, feel free to reply to this post.


Adult!

2014-09-21 23:41:18 by Zanzlanz

Oh hey Newgrounds! I totally forgot to mention it... but I'm 18 years old now!

Yeahaha, I'm an adult now! I turned 18 on September 3rd. I kind of feel the new responsibility of being an adult, but I'm definitely the same Zanz on the inside. I did spontaneously grow a messy mustache and beard though, so that's quite elderly of me.

So also in my life, college is going pretty well! So far I have 100% in all of my classes. My workload is kind of up there, with 18 credits this semester, but I think I'm handling it well.
I'm meeting a lot of nice people - I've even joined the student-run game development organizationthing here at the school. They use Unity, and I'm not sure how happy I am about that. But whatever, it's pretty cool. We're working on a game called Paper Dream.
Also, in my programming class we are learning C++, which is something I've been really wanting to learn for a while now!

So more relevant to my games: I've finally fixed an issue with the development of Lab Lights 2. I am a bit hung up on a random stack-overflow glitch in the Mine Blocks AS3 port. I did a little bit of work on ZanzCode so now its interpreter isn't totally useless. I totally forgot what I was last doing in MB2 so I haven't touched that in months (sorry).

Well that's about it. Take care, my amazing friends and followers,
- Zanzy

Yeeu whippersnappers get off meh lawwn! ;)


Lab Lights 2, and College!

2014-08-24 17:57:47 by Zanzlanz
Updated

Hey everyone!

I'm a college student now. I'm living on campus with 5 other roommates at a technical university. I'm getting a degree in Computer Science. More specifically, my courses revolve around game software development. I'm going to college for game dev!! WOOT!

Anyway, things are pretty insane. My progress will be greatly fragmented. I'll probably focus on the Mine Blocks AS3 port (porting Mine Blocks will take a long time).

I'm also working on a new game! Yep! Lab Lights 2!! Here's an image announcing it:

Lab Lights 2

This image isn't really showing much of what will be new. I don't want to spoil everything, but there's at least be another "player" and some cool mechanics with lasers. :3

Stay tuned! I can't wait to get this game going further. The hardest part right now is coding those 2x1 crates. Pushing them up and down stairs? So many corner cases!

Well anyway, that's about it! Obviously I've been too busy to do Ludum Dare (first time I've missed it since I started participating), but hopefully Lab Lights 2 will make up for that.

Thanks! Comment and stuff if you want - it'll help me get used to working from college. Since technically game dev is my hobby-job... jobby. Sorry, haha bye. XD
- Zanz


Mine Blocks 1.26!

2014-03-28 19:16:03 by Zanzlanz
Updated

Hey Newgrounds!

Mine Blocks 1.26 has just been released!

It now has skins, minecarts, slimes, lemons, dozens of bug fixes, and a huge improvement to performance! I hope you guys enjoy! :D View the entire changelog

 

Play Mine Blocks on Newgrounds!

 

Thanks so much for the constant support everyone! Have fun with the update! :D

- Zanzlanz


Released an Album!

2014-03-07 19:54:03 by Zanzlanz
Updated

Hey hey hey!

I released my first ever album, The Place That Takes Shapes! It was made all in February for FAWM. It's hyper, it's weird, it's me in a nutshell! In general, it's Trance, Orchestral, Chiptunes and Dubstep.

(View on SoundCloud or Bandcamp)

3880797_139423958353_soundcloud1.png   3880797_139423960983_bandcamp1.png

Here's the mix and visualizer I made for the release! Enjoy!

Also - and I'll put the bad news at the bottom so no one notices it - I'm not going to Pico party this year. It breaks my heart but I guess last year was all I could ask for anyway! I'll stop by the chat to contribute a happy emoticon or two :)

Peace!

- Zanz


1000 fans!

2014-01-30 19:25:39 by Zanzlanz
Updated

WOAH. WOAH! :O

Guys!! The 1000 Newgrounds fan milestone has been achieved! Whoohoo! *Throws confetti*

This is a great opportunity for me thank you all so much for being my fans and followers! I am seriously super thankful for your support and your feedback! I am often inspired and encouraged by you guys to improve my abilities and make more awesome stuff! :D


Here's some information on what I've been up to lately:

What's going on with Mine Blocks?

Mine Blocks was put on hold for a while to make room in my schedule for Mine Blocks 2. But now I've been working on it again! I've added a really exciting feature that you guys have been requesting for ages... and I'm dieing to release it! xD We're getting closer to an update, but there's still a lot of stuff to organise first. :)

What's going on with Mine Blocks 2?

Mine Blocks 2 seemed to be going smoothly, but I've run into a lot of small issues that make it practically impossible for me to continue smoothly. I've been attempting to learn C++ these past few days, but it's extremely frustrating. If I can't push myself to learn C++, I might try Java. Bleh, it just stresses me out. I'll try to take it easy for a while. ;\ I just don't like seeing that April Fools update the last thing live, and I just feel really bad for not being able to keep up with it :C

Secret Box - SpongeBob remix - Listen!

If you're a SpongeBob fan, you might like that 'dubstep' remix of a classic SpongeBob episode. If you're not a SpongeBob fan, it probably won't make much sense, but you might like it anyway. :3

ZanzCode

Well ZanzCode is a project I've been working on. It's going okay! I can't wait to make some good progress in it. ;)

What else is going on?

- Well next month is FAWM - February Album Writing Month! I might try it out if I have enough time. Making 14 tracks in 28 days sounds enticing, but I don't know if I can manage it at my level. We'll see! x)

- Also, I'm still extremely interested in a Convey sequel, and I have a great concept for it that I want to start working on soon. Again, your feedback is really encouraging and I definitely want to keep giving you guys more of what you enjoy playing ;D

- The concept for a Str1ngle sequel will need some thought before I'm going to start it. I have a lot of work on my plate, and starting ANOTHER new project right now will be a bad idea.

- School. xD


Welp that about sums it up! Thanks again everyone! You guys really rock!!

Zanz


Everything I did in 2013

2013-12-30 19:39:19 by Zanzlanz
Updated

Happy New Year Newgrounds! It's really been another fantastic year for me.

In summary, I made 4 games, 10 real tracks, a couple websites, and one amazing trip to celebrate Pico party. I've gained a lot of knowledge and made a lot of awesome friends. Thank you so much, everyone, for being so supportive and kind!

I wrote a huge post on my website about everything I did this year. Check it out; it's color-coded and organized!

>> Everything I did in 2013 <<

I hope 2014 is another fantastic year!

See you there! - Zanzlanz


Convey & Texture Tiler

2013-09-17 17:05:35 by Zanzlanz
Updated

Hey Newgrounds!

First of all, I'd like to thank you guys for your really kind feedback on my Ludum Dare submission, Convey. I would have never thought I'd get 32nd place out of 2213 games, in Ludum Dare.
You guys are also responsible for getting me my first daily feature. So thank you! Your support means a lot! <3
And thank you Tom, as well, for putting Convey on the front page! I highly doubt the game would have gotten the rating it did without your help.

*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._

Attention game devs!
I made a simple site to help you quickly and easily test how well your textures tile in your browser! It's meant to be a convenient way to check for seamless tiling, and it's already helped quite a few people!

Just paste, drag and drop, or load the image into the site, and it instantly tiles it for you. Also, control the scale of the texture to see details or the big picture.

-- TextureTiler.com --

It works best in Firefox or Chrome, but does not support IE.

There's a Twitter page for it too, if you're interested in receiving update notices, or to send direct feedback on the site: Twitter.com/TextureTiler.

*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._.-=*=-._

Lastly, I want to mention that my site is working properly again. For a few weeks it's been acting slightly odd... but now it's back to normal. Sorry to everyone that was inconvenienced by the issues; it is definitely my top priority to keep my content running and in healthy condition! :D

Thanks again!
- Zanz


Mine Blocks 1.25 Released!

2013-05-13 19:52:36 by Zanzlanz
Updated

Hello!

Mine Blocks 1.25 was released yesterday! I hope you enjoy!
Play Mine Blocks 1.25
(2D Minecraft in Flash)

The major new features include:
- Enchanting!
- Afros!
- Stairs and half slabs!
- Spawn eggs!
- Dispensers!
- Better redstone!
- Tons of bug fixes!
See full change log.

.
/* */


Ludum Dare and Pico Party

2013-04-29 20:16:03 by Zanzlanz

Hi Everyone!

Wow it was amazing to meet all of you talented, awesome people at the Newgrounds Pico Day Party! I really enjoyed the experience. I hope to do it again next year, definitely. :D

So, as some of you saw at the party, I was working on my Ludum Dare submission. I finished the prototype. Feel free to check it out below. I will want to post it on Newgrounds when I'm actually done with the game, which is probably going to be quite a while.

http://www.ludumdare.com/compo/ludum-dare-26/?ac tion=preview&uid=11407

http://zanzlanz.com/GamePage?n=Just%20Shapes%20L D

Thanks to everyone, especially Tom and his crew, for creating such an amazing community. Happy Pico day! <3

- Zanz

Ludum Dare and Pico Party