Finding the Intersection of Two Lines
November 1, 2009
For lines that aren’t vertical.
1 2 3 4 5 |
For any linear line:
1 2 3 4 5 6 7 |
Say you have 2 lines 5x+2 and -x-1.
Using slope-intercept form(ax+b), we can fill in the parameters.
5x+2:
a = 5
b = 2
-x-1:
a = -1
b = -1
So to find the point at which these 2 lines meet, you would call the function like so:
1 | findIntersect(5, 2, -1, -1) |
It would trace:
(x=-0.5, y=-0.5)
If you want to have a vertical line, put the slope as NaN and the y-intercept as whatever you want y to equal to.
So y = 5 would be:
a = NaN
b = 5
Finding Out What Sets a Variable
October 31, 2009
If you ever need to find out what accesses a variable, then there is a simple thing you can do.
For example say you have an example called ‘charHP’.
If you want to find out what accesses this variable, remove ‘var charHP:int’ or whatever instantiation you have and add this:
1 2 3 4 5 6 |
The next time the variable is accessed, an error will be thrown that shows the whole hierarchy of what set charHP. To see it in a better format, use the Debug Player by compiling CTRL+SHIFT+ENTER. If you want to find out what is reading the value, make the error thrown in the GET function.
How to Sitelock a Flash File
October 4, 2009
1 2 3 4 5 6 7 8 |
That piece of code gets the domain name of the website the flash file is on and compares it to the site you want it sitelocked to.
If the domain isn’t the one the file is sitelocked to, it throws an error.
You might want to use some type of encryption method to hide the domain name.
Change allowed_site to your domain.
How to ‘send an object to top’
September 26, 2009
Sending an object to top is pretty simple. You only need one line of code that will work for all objects.
What you have to do is set the depth of the object to the highest possible, which will make it appear on top.
Put this line of code where ever you want your object to go to the top:
Let’s analyze this code.
First I call setChildIndex from the parent of the object. setChildIndex is what you use to change the depth of an object. You have to call setChildIndex from the parent, so I do Object(parent). The first parameter is the child who’s index you want to set. Then the second parameter is the index you want to set it to.
We want to set the index to the max, so you have to get the amount of objects in the same level as the object. You get that by Object(parent).numChildren. The Display List operates on a 0+ index though, so we have to do numChildren-1 or else we’ll get index out of bounds.
So what we do in that line is set that object’s index to the max possible.
A trick for setting the minimum/maximum of a variable
September 1, 2009
Normally if you want to limit a variable to something, you would use an if statement. Like so:
1 2 3 4 5 6 7 8 9 10 | //Limit to maximum of 10 if(myVar>10) { myVar = 10; } //Limit to minimum of 0 if(myVar<0) { myVar = 0; } |
Instead of that, you could simple use Math.min and Math.max. This way:
1 2 3 4 5 6 | /*Math.min returns the smaller number, so if 10 is smaller than myVar, myVar is bigger than 10. So it returns either 10, or myVar if it is less than 10*/ myVar = Math.min(myVar, 10); /*Math.max returns the bigger number, so if myVar is less than 0, it'll return 0, otherwise if myVar is bigger than 0, it'll return myVar*/ myVar = Math.max(0, myVar); |
Neat trick, eh?
But this should only be used if speed doesn’t matter in your project.
A better method is to use the ternery operand.
Which would be used to limit like this:
1 2 | myVar = ((myVar>10)?10:myVar);//limit to max of 10 myVar = ((myVar<0)?0:myVar);//limit to min of 0 |
A nifty thing about trace
September 1, 2009
Trace allows multiple parameters. In ActionScript 2 you could only trace one thing at a time. If you wanted to add more, you had to add it with +. In AS3, you just put it as a different parameter.
For instance:
1 | trace(x, y); |
If you put that in the first frame, you’ll get
0 0
It seperates each with a space.
Cool right?
Oh and if you’re wondering what trace is used for, it is used for debugging your flash application/game.
How to Check if MovieClip Exists
September 1, 2009
Have you ever had a piece of code where you might need to remove a child from the display list at a moments notice? Then you know that if you try to remove an object that’s already been removed, you get an error.
This is where contains comes in.
It’s simple to use it. You just do:
1 2 3 4 | if(contains(theMC)) { removeChild(theMC); } |
More Efficient Massive Circle Collision Testing
September 1, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | package{ import flash.display.Sprite import flash.events.Event import flash.geom.Point public class circle extends Sprite{ private var vel:Point = new Point(); private var index:int = new int(); public function circle():void{ var w:int = new int(Math.random()*50+10); while(vel.x == 0 || vel.y == 0) { vel.x = int(Math.random()*20-10); vel.y = int(Math.random()*20-10); } vel.x*=2; vel.y*=2; graphics.lineStyle(1); graphics.drawCircle(0,0, w); addEventListener(Event.ENTER_FRAME, onEnter); addEventListener(Event.ADDED_TO_STAGE, onAdd); } private function onAdd(e:Event):void{ index = parent.getChildIndex(this); removeEventListener(Event.ADDED_TO_STAGE, onAdd); } private function onEnter(e:Event):void{ x+=vel.x; y+=vel.y; //Collision Checking for(var i:int = new int();i<parent.numChildren;i++) { var obj:Object = parent.getChildAt(i); //dont need abs if(i != index && Math.sqrt(Math.pow(x-obj.x, 2)+Math.pow(y-obj.y, 2))<=obj.width/2+width/2) { vel.x = -vel.x; vel.y = -vel.y; } } //Boundary Checking if(x+width/2>stage.stageWidth) { x = stage.stageWidth-width/2; vel.x = -vel.x; } if(x-width/2<=0) { x = width/2; vel.x = -vel.x; } if(y-height/2<=0) { y = height/2; vel.y = -vel.y; } if(y+height/2>=stage.stageHeight) { y = stage.stageHeight-height/2; vel.y = -vel.y; } } } } |
Save as circle.as in same folder as FLA file.
Put this code in FLA file:
1 2 3 4 5 6 7 8 9 | var i:Timer = new Timer(1000); i.addEventListener(TimerEvent.TIMER, addBall); i.start(); function addBall(e:TimerEvent):void{ addChild(new circle()); if(numChildren>5)i.stop(); } |
It creates a ball every second until you reach 6 balls.
Now the circles overlap a lot and sometimes they get stuck, but that’s mostly because writing code that will make sure they don’t overlap and that will make them not get stuck will take too long. It IS possible though.
The way that I use is this:
I get the distance between the centers of two circles. Let’s say that is dist. Then I add up the width/2 of both of them.
Then I do an if statement.
1 2 3 4 | if(dist<=obj1width/2+obj2width/2) { //hit detected! } |
Instead of that huge for loop(using hitTest with specific coordinates), you use the distance formula. Neat eh?
This method requires a bit of knowledge in algebra.
The distance formula in flash goes like this(in AS3):
Variable types and their different uses(in games)
September 1, 2009
First on the list, array(ironically).
To declare an array, you do:
A practical use would be to have a list of enemies in a game. If you ever want to do something to all the enemies currently added on to the stage, you would simply loop through it. NOTE: Arrays can hold any data type you want. A reference to an enemy, a list of numbers, anything.
Next:
The use for this has a very wide range. You use numbers for everything. In fact, your processor works with numbers(binary code). You could use an int to hold the hp variable.
Boolean:
You have 2 choices for the parameter. True or false. This data type can be used for simple stuff like dead. If you want to let another class know if the character is dead, just set dead = true when hp<=0.
String:
The example used above shows one use of the String data type. You can use it to hold the character name they choose.
How to make a button in AS3
September 1, 2009
There were two ways you could’ve made a button in AS2. Putting the code on button itself with an on(release) event or putting it in the frame with a button_btn.onRelease event.
The ActionScript 3 way is similar to button_btn.onRelease. Similar, but barely. The first way is impossible because you cannot put code on symbols anymore. Only in frames and classes.
First you draw your button in Flash. Then highlight the button and press F8.
Change the information to the image below:
I’ve circled the information you need to change.
If you don’t see the image, change Type to Button and name to button_btn.
Convert to Symbol
Now you’ve made your button.
If you compile your movie right now, you’ll see that the button is ‘clickable’, as in the cursor changes to the click one.
If you double click on the button, your timeline will change. It will look like this:
Timeline for a button
Here’s an explanation of all four of the frames:
Up: This is how the button looks like when the mouse isn’t hovering over the button.
Over: This frame is shown when you mouse over the button.
Down: This frame is shown when you mouse down on the button. As in you press down but don’t let go.
Hit: This was confusing to me at first, but it’s rather simple really. Most of the time you leave it alone, but if you need the button to be clickable in a specific spot only, you use this. It’s hard to explain so let me just show you. Put a box half the size of your button inside the hit frame. Then compile your movie and you’ll notice that where the box was is the only part that’s clickable. If you leave this box empty, everything in the button symbol will be clickable.
You can leave the last three frames empty. It’ll just show the Up frame at all times.
The next step is to give the button an instance name. Click on the button only once.
Then go to your properties panel.
Properties Panel
I’ve circled what you need to change. You need to change the instance name. This is what ActionScript uses to have access to the object with code. You can put the name as whatever you want, but I’ll be using button_btn.
Then click on the first frame and press F9. This opens up the Actions Panel.
First you need to add an event listener. An event listener is pretty much something that calls a function when the event you make it listener for is called.
Ours will be:
1 |
We’re making it listen for the MouseEvent.CLICK event for button_btn. When the event occurs, it will call the function onClick.
So the next step is to code our onClick function.
1 2 3 |
It’s declared like a normal function, however we have an extra parameter. You don’t have to worry about it. You only need it if you’re accessing information about the mouse. You need to have it present though. onClick is an Event Listener function. Event Listener functions need to have a parameter to hold the type of event they’re calling. We called MouseEvent.CLICK, so we needed a MouseEvent parameter.
When you click on button_btn, you should get
You clicked me!
in the Output Panel.
You can also use buttons to open Web Pages.
Congratulations, you’ve made your first button!
