Showing posts with label AS3. Show all posts
Showing posts with label AS3. Show all posts

Friday, October 9, 2009

Case 04 - Final

Hello.

This is the final report about the fourth case. I haven't had so many posts for the third and this case, this is because I don't really feel the need of writing about stuffe every other day. As it is getting more difficult, and me not being a born programmer I don't have breakthroughs every day. So I feel it's better is I make an inital post for every case and the sum up how it all went at the end of the week when I've (hopefully) managed to get to the geal :)

So about this case. I'm still having a hard time understanding all the stuff we're going through. Apparently I'm not the born programmer ^^
Anyway, with help from classmates I've managed to get things done. I may not yet fully understand everything, but it feels like I'm slowly learning stuff. I now know that I gotta have a package, I gotta have variables, functions that define the variables, and umm... yeah :)
I'm going to try to explain to you what I've done so far and give you a view of my product so you can test it out for yourself, beacause, yes I was able to make my FlashBob controlable with the arrowkeys :p

First of all I copied my previous case files into an new folder wich I called Case 04. Next up I got rid of fragments of the code that made FlashBob move by himself. Then I had to make new variables. This is what I did:
  • I left the variable for my character be. Then I made variable Booleans (wich basically checks if something is true or not later on). Then I made a variable to define the speed of FlashBob's movement. Set it to 15, wich again means he'll move by 15 pixels, unless I define something else in the function later on.
  • Next up i had to add Event Listeners, these "listen" if any buttons are pressed. And I set it the to check if a KeyIsDown and if a KeyIsUp.
  • Then I added a function that basically stops FlashBob from running out of the stage. This is done by adding conditionals wich ask a question. And the answer decieds what to be done. In this case I told AS that if the position of FlashBob is greater og smaller than the stage width it has to block him.
  • I made a function that decides how FlashBob moves. This is done again by adding coditionals. Since I'll have FlashBob run both to the right and the left I have to add to coditionals, one for each direction. So I got one called leftKeyIsDown and righKeyIsDown.
    • Then I define on what axis I want FlashBob to move, on this case the X axis and then the speed. Since I already have made a variable for that I just refer to it by writing += speed and -=speed. This states that if the left key is down FlashBob will move to the left since it's -= speed, and move to the right if the right key is down because the speed is set to += speed.
    • In the same contitional I also define what frame in my sprite I want to play, my running animation is on the second frame so I tell AS to gotoAndPlay(2).
    • Now, I don't want him to run forward and backward, I want him to always face the direction he's running. This is done by adding another line under the gotoAndPlay. Here I tell AS that then he is running to the left I want to flip FlashBob on the X axis, I do that by writing scaleX = -1. When running to the right i write scaleX = 1. The reason I don't have a + sign is that the original animation of FlashBob is running to the right, so there's no need to flip him over or change his scale.
  • Now I need a function that checks if the keys are pressed. To do this I first make a function that checks if the keys are pressed. And another one to check if the keys arent pressed. I then add the keys I want to use by adding the Key Code of each button. Key Code Overview 
And that's about it. I had a great deal of help on this one... again. I was already having some problems on the previous case, and this was by no means easier. But I've learned a great deal already.
Finally you can check out the product of my hard work here.


Case 04 by ~Elr0hiR on deviantART

On a side note I figured out to add music. You won't hear it it in the preview though. I'm saving this one for the finished game :)

Saturday, September 26, 2009

Case 03 - Final

This is the last post about the third case. As mentioned earlier the scope was to use ActionScript to program the sprite, making it move back and forth in the picture "bouncing" off the edges.

It wasn't really before thursday that I figured out how to do it, all my other trys the days before failed as said. But when we were going through the case with the teacher and a lab assistant they got the same problem I had struggled with the last three days. This was a good thing, at least for me, 'cause then the teacher and assistant had to figure out what was wrong, and by doing that, they showed how to sovle the problem I'd been having.

So when I finally had succeeded in writing a code that made a box bounce off the walls, I changed the code so that my character would replace the box. This was pretty nice because for the first time that week I felt that I managed to get something right.  ^___^
Steffen aslo showed my how to flip the character so it wouldn't run backwards. That's pretty easy, you basically just add the if command and tell the character to .scaleX = -1 one way, and .scaleX = +1 the other way.

Anyway, now I'll tell you more specifically what I did to make this work and complete the third case.
  • First of all I created a new folder and called it "Case 03". This folder was my container for the animation (.fla file) and the script file (.as file).
  • Then I copied my flash file with the animations and my sprite into the folder.
  • Next thing to was was to open Flash and create an ActionScript File. That was the file in wich I wrote the code. Then I savd it as "Document.as" in the Case 03 folder. For this to work the .as file and the .fla file must be in the same folder, otherwise the script can't access the objects in the flash library and make the magic happen.
  • Next up was to ensure that the script file could associate with the .fla file, to do this I went to the properties of my flash document and named the Class of the file to Document - but without the .as at the end. As far as I've understood this has to be done if not the files won't communicate correctly and you'll encounter problems when the script is trying to use ojects from the library.
  • The last thing that had to be done before starting to write the code was to make sure the oject you plan on using can be accessed by the actionscript. To do this I went to the properties page of my walk animation (wich I had decided to use for the experiment) and made sure that:
  • The type of the objetct was Movie Clip.
  • It was ready to get exported for ActionScript.
  • Aslo export in frame 1.
  • Check that the the Class was Walk (the name of the animation).
  • Check that the base class is flash.display.MovieClip.
  • And finally choose the source almost on the bottom of the properties page. To do this I pressed "Browse", and found the .fla file I was working with. When I chose it a new window popped up with the objects in the library, I simply then marked Walk and clicked "Ok".
    • Now all the preparations was done and I could start to write the code.
    • What I did was to follow what the teacher told during the lab while the assistant showed what he wrote on a projector. I tried to write stuff by myself also, to see if I could remember anything from the books I read. Wasn't much though. It's still pretty overwhelming stuff for me :)
    • After writing the code, I added a shadow and made it follow the character while it moved from side to side. But took it away, and made the character turn around each time it reached a border instead, thanks to Steffen ;)
    You're probably getting bored of reading my post now, so I'll add a preview of the flash file with the character running back and forth.



    Case 03 by ~Elr0hiR on deviantART

    Friday, September 25, 2009

    Case 03

    Sorry I'm a little late on this case.
    Here's the first thoughts about my third case :)
    And it's all about ActionScript 3.0. My third case is to move a character on the screen without leaving it, only using AS3.

    My first thoughts about this were that it was going to get interresting. I didn't have any experience with programming, so I had absolutely no clue how it works. I mean, at the time I didn't even know what a code looked like..

    So on monday we got the case, and being eager to take on the new case I spent a couple of hours after school trying to figure out things. I read through the first chapter of Oriley's Learning ActionScript 3.0 A beginners Guide without understand much. It went over the basics, so at least I understood what some of the functions of the code does. Gotta read it again though. Anyway, after reading a bit I looked at the exaple files provided by the teacher and lab assistants and tried to figure out what went on. I tried to implement the example code to my own character, but it failed. After a couple of trys I figured I needed a break so I let it rest for the day.


    The following days I still failed miserably at programming the character to move forth and back on the screen.

    Here's what I was able to do the first couple of days:


    HALP by ~Elr0hiR on deviantART