Be a Supporter!

As: Api

  • 14,777 Views
  • 174 Replies
New Topic Respond to this Topic
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
As: Api 2005-06-26 14:26:38 Reply

API is a way of drawing things using actionscript, it can be used draw simple things but it is difficult to go into much detail because of how it is done.

The codes are pretty basic, the main codes are:

createEmptyMovieClip("instance name", depth) - this makes an empty Movie Clip through actionscript and is needed to use API.

lineStyle(thickness, colour, alpha) - this is used to outline what properties your lines will have

lineTo(x, y) - this actually draws the line. Just input co-ordinates and a line will go from the current position to them.

curveTo(control X, control Y, anchor X, anchor Y) - this draws a curved line, its hard to use but can look good.

beginFill(colour, alpha) - this begins filling withing some lines which you specify using the lineTo command

endFill() - this ends the beginFill command

And then there are other commands such as:

moveTo(x, y) - this moves the current co-ordinate of the MC

with(instance name){ } - this is just used as a shortcut, inside the {'s, you place the lineTo commands and such.

---------

So, an example code. This will draw a simple square, place the code on the first frame of a new .fla file.

_root.createEmptyMovieClip("square", 1);
with(square){
//now we have made our square but it has nothing in it
lineStyle(2, 0x000000, 100);
//the line will be 2 pixels thick, black and 100% visible
beginFill(0x000000, 50);
//the fill will be black, and half visible
//now we will begin drawing
lineTo(50, 0);
lineTo(50,50);
lineTo(0, 50);
lineTo(0, 0);
}

If you test the flash now (ctrl+enter) you will see you have drawn a square using actionscript.

---------

Now, I'm going to do the same square, but using the 'curveTo' method.

_root.createEmptyMovieClip("square", 2);
with (curvedsquare) {
//now we have made our square but it has nothing in it
lineStyle(2, 0x000000, 100);
//the line will be 2 pixels thick, black and 100% visible
beginFill(0x000000, 50);
//the fill will be black, and half visible
//now we will begin drawing
curveTo(25, -10, 50, 0);
curveTo(60, 25, 50, 50);
curveTo(25, 60, 0, 50);
curveTo(-10, 25, 0, 0);
}

If you test that, it will make a square with sides that curve slightly, rather than just being straight.

-------

Ok, so far everything we've done is very simplistic. It would be much easier to do it within flash, and would take less time. The best thing about API is that you can change things dynamically, such as simulate the Windows mass select drag box.

Paste this into frame #1s actionscript panel:

onLoad = function () {
var1 = 0;
_root.createEmptyMovieClip("box", 1);
box.lineStyle(2, 0x0099FF, 100);
};
//when the flash loads, it will create a new variable called 'var1' that holds the number 0 and a new movieclip called 'box'
onMouseDown = function () {
box.moveTo(_xmouse, _ymouse);
var1 = 1;
curX = _xmouse;
curY = _ymouse;
};
//when the mouse is pressed, the box will move to the mouse point and the variables will all change
onMouseUp = function () {
var1 = 0;
};
//when the mouse stops being pressed, the var1 variable will equal 0
onEnterFrame = function () {
duplicateMovieClip("box", "box2", 2);
box2.lineStyle(2, 0x000000, 100);
//the box movieclip will be copied and the copy will be called 'box2'
if (var1 == 1) {
box2.beginFill(0x000000, 25);
box2.lineTo(_xmouse, curY);
box2.lineTo(_xmouse, _ymouse);
box2.lineTo(curX, _ymouse);
box2.lineTo(curX, curY);
}
//when the mouse is down, box2 will be drawn with the co-ordinates supplied.
};

The box will be drew from where the mouse is originally clicked, to where the mouse position is.

-------

You could also make a line from the mouse to a MC, with this:

onEnterFrame=function(){
createEmptyMovieClip("line", 10);
with(line){
lineStyle(2, 0x000000, 100);
moveTo(_xmouse, _ymouse);
lineTo(instance name of MC1._x, instance name of MC1._y);
}
}

------

For more advanced coders (read: people with more time on their hands), you could make an entire game out of actionscript.

1. Set your variables. We need the following variables:

score = 0;
time = 0;
rand1 = random(250)+25;

2. Create your character, I won't go into much detail.. instead I'll use a basic square:

_root.createEmptyMovieClip("char1", 50);
with (char1) {
lineStyle(2, 0xFFFFFF, 100);
moveTo(_x-15, _y-15);
beginFill(0xFFFFFF, 50);
lineTo(_x+15, _y-15);
lineTo(_x+15, _y+15);
lineTo(_x-15, _y+15);
lineTo(_x-15, _y-15);
endFill();
}

3. Now underneath, make three movieclips which will make up the floor and one for the floor colour:

_root.createEmptyMovieClip("wall1", 10);
with (wall1) {
lineStyle(5, 0x000000, 100);
moveTo(0, 200);
lineTo(rand1, 200);
}
_root.createEmptyMovieClip("wall2", 11);
with (wall2) {
lineStyle(5, 0x000000, 100);
moveTo(rand1, 250);
lineTo(550, 250);
}
_root.createEmptyMovieClip("wall3", 12);
with (wall3) {
lineStyle(5, 0x000000, 100);
moveTo(rand1, 200);
lineTo(rand1, 250);
}
_root.createEmptyMovieClip("base", 0);
with (base) {
lineStyle(1, 0x000000, 0);
moveTo(0, 200);
beginFill(0x00CC66, 100);
lineTo(rand1, 200);
lineTo(rand1, 250);
lineTo(550, 250);
lineTo(550, 400);
lineTo(0, 400);
lineTo(0, 200);
endFill();
}

4. Make a coin, this is just a square, basic coin but still, it is something to collect.

_root.createEmptyMovieClip("coin1", 100);
with (coin1) {
lineStyle(2, 0xFFFF00, 100);
beginFill(0xFFCC00, 100);
lineTo(_x+10, _y);
lineTo(_x+10, _y+10);
lineTo(_x, _y+10);
lineTo(_x, _y);
endFill();
_x = random(400)+50;
_y = random(180);
}

***TO BE CONTINUED IN NEXT POST***


Sup, bitches :)

BBS Signature
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to As: Api 2005-06-26 14:36:00 Reply

5. Now to apply the actions to the character.

onEnterFrame = function () {
time += 1;
if (time == 600) {
time = 0;
score -= 10;
}
//each 60 seconds, the score goes down by 10
if (0>score) {
char1.removeMovieClip();
char2.removeMovieClip();
_root.createEmptyMovieClip("lose1", 100000);
with (lose1) {
lineStyle(3, 0x000000, 100);
lineTo(0, 50);
lineTo(5, 50);
moveTo(10, 50);
lineTo(10, 0);
lineTo(30, 0);
lineTo(30, 50);
lineTo(10, 50);
moveTo(55, 0);
lineTo(35, 0);
lineTo(35, 25);
lineTo(55, 25);
lineTo(55, 50);
lineTo(35, 50);
moveTo(80, 0);
lineTo(60, 0);
lineTo(60, 50);
lineTo(80, 50);
moveTo(60, 25);
lineTo(80, 25);
}
}
//if score is below 0, then the player loses and the word 'lose' is drawn on screen.
if (char1.hitTest(wall1) || char1.hitTest(wall2)) {
} else {
char1._y += 3;
}
//adding gravity
if (Key.isDown(Key.LEFT)) {
if (char1.hitTest(wall3)) {
} else {
char1._x -= 5;
}
}
if (Key.isDown(Key.RIGHT)) {
char1._x += 5;
}
if (Key.isDown(Key.UP)) {
char1._y -= 5;
}
// so the player can move
if (char1.hitTest(coin1)) {
coin1._x = random(400)+50;
coin1._y = random(180);
bell1 = new Sound();
bell1.attachSound("ding1");
bell1.start(0, 1);
bell1.setVolume(random(200)+50);
score += 1;
}
//if the player touches the coin, the coin moves somewhere else and the score goes up by 1, also if you have a song/SFX in your library then right click on it and goto linkage, tick the first box and change the name to 'ding1'.
if (Key.isDown(Key.SPACE)) {
trace("Your score is "+score);
}
};
//if SPACE is pressed, the score will be traced.

-------

So, that is a really basic game that I made in about an hour, it's pretty pointless and rather difficult but is a pretty good example of how a game can be completly made with words.

The whole code to make the game is below:

onLoad = function () {
time = 0;
rand1 = random(250)+25;
_root.createEmptyMovieClip("wall1", 10);
with (wall1) {
lineStyle(5, 0x000000, 100);
moveTo(0, 200);
lineTo(rand1, 200);
}
_root.createEmptyMovieClip("wall2", 11);
with (wall2) {
lineStyle(5, 0x000000, 100);
moveTo(rand1, 250);
lineTo(550, 250);
}
_root.createEmptyMovieClip("wall3", 12);
with (wall3) {
lineStyle(5, 0x000000, 100);
moveTo(rand1, 200);
lineTo(rand1, 250);
}
_root.createEmptyMovieClip("char1", 50);
with (char1) {
lineStyle(2, 0xFFFFFF, 100);
moveTo(_x-15, _y-15);
beginFill(0xFFFFFF, 50);
lineTo(_x+15, _y-15);
lineTo(_x+15, _y+15);
lineTo(_x-15, _y+15);
lineTo(_x-15, _y-15);
endFill();
}
_root.createEmptyMovieClip("char2", 49);
with (char2) {
lineStyle(2, 0xFFFFFF, 50);
moveTo(_x-15, _y-15);
beginFill(0xFFFFFF, 30);
lineTo(_x+15, _y-15);
lineTo(_x+15, _y+15);
lineTo(_x-15, _y+15);
lineTo(_x-15, _y-15);
endFill();
}
_root.createEmptyMovieClip("coin1", 100);
with (coin1) {
lineStyle(2, 0xFFFF00, 100);
beginFill(0xFFCC00, 100);
lineTo(_x+10, _y);
lineTo(_x+10, _y+10);
lineTo(_x, _y+10);
lineTo(_x, _y);
endFill();
_x = random(400)+50;
_y = random(180);
}
_root.createEmptyMovieClip("base", 0);
with (base) {
lineStyle(1, 0x000000, 0);
moveTo(0, 200);
beginFill(0x00CC66, 100);
lineTo(rand1, 200);
lineTo(rand1, 250);
lineTo(550, 250);
lineTo(550, 400);
lineTo(0, 400);
lineTo(0, 200);
endFill();
}
score = 0;
score.txt = 0;
};
onEnterFrame = function () {
myRadians = Math.atan2(char1._y-char2._y, char2._y-char2._x);
_root.yChange = Math.round(char1._y-char2._y);
_root.xChange = Math.round(char1._x-char2._x);
_root.yMove = Math.round(_root.yChange/1.5);
_root.xMove = Math.round(_root.xChange/1.5);
char2._y += _root.yMove;
char2._x += _root.xMove;
time += 1;
if (time == 600) {
time = 0;
score -= 10;
}
if (0>score) {
char1.removeMovieClip();
char2.removeMovieClip();
_root.createEmptyMovieClip("lose1", 100000);
with (lose1) {
lineStyle(3, 0x000000, 100);
lineTo(0, 50);
lineTo(5, 50);
moveTo(10, 50);
lineTo(10, 0);
lineTo(30, 0);
lineTo(30, 50);
lineTo(10, 50);
moveTo(55, 0);
lineTo(35, 0);
lineTo(35, 25);
lineTo(55, 25);
lineTo(55, 50);
lineTo(35, 50);
moveTo(80, 0);
lineTo(60, 0);
lineTo(60, 50);
lineTo(80, 50);
moveTo(60, 25);
lineTo(80, 25);
}
}
if (char1.hitTest(wall1) || char1.hitTest(wall2)) {
} else {
char1._y += 3;
}
if (Key.isDown(Key.LEFT)) {
if (char1.hitTest(wall3)) {
} else {
char1._x -= 5;
}
}
if (Key.isDown(Key.RIGHT)) {
char1._x += 5;
}
if (Key.isDown(Key.UP)) {
char1._y -= 5;
}
if (char1.hitTest(coin1)) {
coin1._x = random(400)+50;
coin1._y = random(180);
bell1 = new Sound();
bell1.attachSound("ding1");
bell1.start(0, 1);
bell1.setVolume(random(200)+50);
score += 1;
}
if (Key.isDown(Key.SPACE)) {
trace("Your score is "+score);
}
};

The game is a total of 4.6kb.


Sup, bitches :)

BBS Signature
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to As: Api 2005-06-26 15:34:05 Reply

I bet curveTo was used in RaidenX as the purple beam.

HalfAssed
HalfAssed
  • Member since: Jun. 24, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to As: Api 2005-06-26 15:55:59 Reply

You can also do 3d effects with just actionscript.

Here is an example that I have made.

ActionScript 3D

(you may have to click on the flash before space bar will work)

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Api 2005-06-26 16:03:20 Reply

A nice reference there Liam, I was hoping someone would do an AS:API. Thanks

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Api 2005-06-26 16:05:09 Reply

At 6/26/05 03:55 PM, HalfAssed wrote: You can also do 3d effects with just actionscript.

Here is an example that I have made.

ActionScript 3D

Thats amazing! I gotta get better at this stuff...

HalfAssed
HalfAssed
  • Member since: Jun. 24, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to As: Api 2005-06-26 16:59:41 Reply

At 6/26/05 04:05 PM, T-H wrote: Thats amazing! I gotta get better at this stuff...

Thanks. As you might have guessed, I based it off the script for a single, grey, rotating cube that appears every now and then in AS tutorials.

MouthWindow
MouthWindow
  • Member since: Dec. 4, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Api 2005-06-28 09:00:30 Reply

when i wanna get back into flash ill refer back here

Go0gley
Go0gley
  • Member since: Jul. 8, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to As: Api 2005-06-30 10:06:28 Reply

You are right. But I also put vibrations, flashes and other lightning effects.

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Api 2005-07-08 14:04:21 Reply

I've taken my first adventure into API today. I've been dreading it, but I'm actually really enjoying the coding. Haven't quite got used to curveTo yet, but I'm quite happy with my first completely dynamically created game (so far, more to add yet).

THROW IT

184 lines of code so far, filesize: <2 kb !!! =)


- - Flash - Music - Images - -

BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As: Api 2005-07-08 14:06:47 Reply

cool :) add objects in the air it can jump on and stuff

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Api 2005-07-08 14:43:57 Reply

Wow, awesome stuff Denvish, I love the grass.

I'll add something small that I've learned to do when dynamically drawing circles. It gives a much rounder looking circle than when using curveTo (as perfect circles are impossible to create either way, but this method seems to give a niver result.)

_root.createEmptyMovieClip("circle_mc",_ro
ot.getNextHighestDepth());
circle_mc.lineStyle(200, 0x000000,100);
circle_mc.moveTo( 0,0);
circle_mc.lineTo(0.2,0);

Basically, this just uses circle_mc to draw a really short really thick single line, that works surprisingly well as a circle.

Only drawback here is you can't add any art or detail besides the base line colour, so wouldn't work to draw something like Denvish's ball.

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to As: Api 2005-07-08 14:49:32 Reply

At 7/8/05 02:04 PM, Denvish wrote: THROW IT

Thats awesome, I think it's more fun creating API stuff than it is anything else in flash. Maybe thats just my opinion :)


Sup, bitches :)

BBS Signature
Galactic-Shit-Head
Galactic-Shit-Head
  • Member since: Jun. 30, 2005
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to As: Api 2005-07-08 14:55:42 Reply

At 7/8/05 02:04 PM, Denvish wrote: THROW IT

My high score: 62735

lol

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Api 2005-07-08 14:59:38 Reply

At 7/8/05 02:55 PM, Galactica wrote: My high score: 62735

Pitiful, 150422

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Api 2005-07-08 15:00:29 Reply

At 7/8/05 02:59 PM, T-H wrote:
At 7/8/05 02:55 PM, Galactica wrote: My high score: 62735
Pitiful, 150422

Ha!That's nothing! 19355


BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Api 2005-07-08 15:05:51 Reply

At 7/8/05 02:59 PM, T-H wrote:
At 7/8/05 02:55 PM, Galactica wrote: My high score: 62735
Pitiful, 150422

lol, 188406

I think I'm going to add some ground & air 'boosters' and an online scoretable and submit this. It's frighteningly similar to Kitty Cannon, but it's coded from scratch with API, which I don't think Kitty Cannon was.

Once I'm done with it, I'll post the game code as it stands now, so people can use it for reference if they want.


- - Flash - Music - Images - -

BBS Signature
Galactic-Shit-Head
Galactic-Shit-Head
  • Member since: Jun. 30, 2005
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to As: Api 2005-07-08 15:10:02 Reply

Denvish, you ARE a flash god!

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Api 2005-07-08 15:13:09 Reply

OMG!New record!
I found a little glitch,as it's not a full size game,you can drag it outside the stage and throw it really far.
By the way,it couldn't be photoshopped since Denvish used a gradient for the sky - meaning that I really got 32 982.

As: Api


BBS Signature
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Api 2005-07-08 15:17:11 Reply

At 7/8/05 03:13 PM, -Toast- wrote: By the way,it couldn't be photoshopped since Denvish used a gradient for the sky - meaning that I really got 32 982.

You still need to work on your mouse throwing arm, I suggest masturbation.

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Api 2005-07-08 15:18:24 Reply

Hahaha..It's not the same kind of movement though :P


BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Api 2005-07-08 15:20:46 Reply

At 7/8/05 03:13 PM, -Toast- wrote: OMG!New record!
I found a little glitch,as it's not a full size game,you can drag it outside the stage and throw it really far.
By the way,it couldn't be photoshopped since Denvish used a gradient for the sky - meaning that I really got 32 982.

Hmmm, I'm wondering why your text is all black, it should be white and red.
Don't think I'll bother killing that glitch, seems like fair play to me...


- - Flash - Music - Images - -

BBS Signature
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to As: Api 2005-07-08 15:21:59 Reply

At 7/8/05 03:18 PM, -Toast- wrote: Hahaha..It's not the same kind of movement though :P

It COULD be... ¬¬


Sup, bitches :)

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Api 2005-07-08 15:24:52 Reply

Here's the code snippet I used for the gradiated background:

Stage.scaleMode="showAll";
SW=Stage.width; SH=Stage.height;
with(_root){
beginFill(0x000066,100);
moveTo(0,0); lineTo(SW,0);
lineTo(SW,SH); lineTo(0,SH);
lineTo(0,0);endFill();
}
_root.createEmptyMovieClip("fbk", 1000);
with(fbk){
incr=Stage.height/100; fbkY=0;
for(i=0;i<101;i++){
beginFill(0x00CCFF,i);
moveTo(0,fbkY); lineTo(SW,fbkY);
lineTo(SW,fbkY+incr); lineTo(0,fbkY+incr);
lineTo(0,fbkY); endFill(); fbkY+=incr;
}
}


- - Flash - Music - Images - -

BBS Signature
Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Api 2005-07-08 15:31:08 Reply

Yeah,I noticed that too.It's because I pasted the screen right into flash.Here's a bigger version in paint,I think it should work properly.

http://img43.imagesh..wootnewrecord8my.jpg

If you still don't believe me,msn me and I'll show it to you :P

msn - darktoaster@hotmail.com

-_- Yeah,I will waste my time just to show I got a good score :P


BBS Signature
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Api 2005-07-08 15:31:13 Reply

Wow thats interesting, I never would have thought to draw using _root (dumbass). And the way you have created the linear fill is really innovative too.

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to As: Api 2005-07-08 15:36:06 Reply

At 7/8/05 03:24 PM, Denvish wrote: Here's the code snippet I used for the gradiated background:

There is a command for API gradients, beginGradientFill, but I haven't used it yet.

At 7/8/05 03:31 PM, -Toast- wrote: If you still don't believe me,msn me and I'll show it to you :P

You just want a mod on your MSN contacts :P I had Ozcar but he hardly goes on.. or he blocked me.


Sup, bitches :)

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Api 2005-07-08 16:11:12 Reply

At 7/8/05 03:31 PM, T-H wrote: Wow thats interesting, I never would have thought to draw using _root (dumbass). And the way you have created the linear fill is really innovative too.

I like for loops. I have a couple of while loops in my code too, which I very rarely use.


- - Flash - Music - Images - -

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Api 2005-07-12 19:56:56 Reply

Initial version finished and submitted here... 1033 lines of code. I LIKE this API business =)


- - Flash - Music - Images - -

BBS Signature
JeremysFilms
JeremysFilms
  • Member since: Feb. 18, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to As: Api 2005-07-12 20:19:13 Reply

Liam, HOW DO I PLAY YOUR GAME?!! Its just a yellow block with some stage stuff. What's the objective, and HOW do I get him to move or w/e?