View Full Version : [FMX] Advanced Help
GhostToast
11-08-2004, 10:16 AM
I have a code to move an object with keys, and it works, but I would like to be able to have it more like a car, having, it turn only if pressing up or down first, and possibly acceleration. I've tried using && stuff in a bunch of different ways with this and some other stuff but cant figure anything out, it just gets errors.
on (keyPress "<Left>") {
currentX = this._x;
this._x = currentX - 2;
_root.bug._rotation = 270;
}
on (keyPress "<Right>") {
currentX = this._x;
this._x = currentX + 2;
_root.bug._rotation = 90;
}
on (keyPress "<Up>") {
currentY = this._y;
this._y = currentY - 2;
_root.bug._rotation = 360;
}
on (keyPress "<Down>") {
currentY = this._y;
this._y = currentY + 2;
_root.bug._rotation = 180;
}
ScientistBlah
11-08-2004, 02:23 PM
I'm not pro with flash but if you posted the errors you're getting, maybe I could help you out.
DarkReality
11-08-2004, 02:54 PM
you have a variable a. This is a constant value which wil be the the car's acceleration rate. then you have something like:
if (Key.isDown(Key.UP)) {
if (v < MaxSpeed) { v += a; }
}
if (Key.isDown(Key.LEFT)) {
if (v > 0) { car.angle -= AngleChangePerFrame; }
}
if (Key.isDown(Key.DOWN)) {
if (v < MaxSpeed) { v -= a; }
}
if (Key.isDown(Key.RIGHT)) {
if (v > 0) { car.angle += AngleChangePerFrame; }
}
//actual movement uses trig functions. Check a tutorial for more info, or I can explain it shortly if you don't get why it works like this:
car._y += v*Math.cos(car.angle);
car._x += v*Math.sin(car.angle);
//deceleration
if (v > 0) { v -= a; }
else v = 0;
v *= friction;
//I dunno if there's a more accurate way to show car deceleration. Play around with the deceleration to get something realistic.
the on (KeyPress...) is unknown to me, and I simply prefer to keep all code within the _root.onEnterFrame function, for the sake of simplicity. I don't recall being able to put on commands in an onEnterFrame function... but MX 2004 might be different.
My not having flash prevents me from testing, so I sadly can't say if this'll work at all.
illrainman
11-08-2004, 03:09 PM
You could put the left and right keypress tests inside an if statement that tests if the speed is not equal to zero. That way when speed = 0, the code for turning is not executed.
eatmorchikin6464
11-08-2004, 06:53 PM
You might want to look at this tutorial:
http://www.flashkit.com/tutorials/Games/Realisti-John_Ban-1042/index.php
The Brown Cow
11-08-2004, 07:00 PM
One of these days I'm gonna have to write a tutorial that actually explains what that trig business means...
eatmorchikin6464
11-08-2004, 07:26 PM
Good idea, but if you just copy the AS, then, you don't need to know! =)
The Brown Cow
11-08-2004, 07:29 PM
Nope, but then you can't apply it to as many things.
For the most part, there's no need to understand it, but it's helpful to know how it works if you want to do something excessive with physics.
eatmorchikin6464
11-08-2004, 07:45 PM
But, I'm that smart when it comes to physics and stuff, so it doesnt do anything for me anyways. I mean, that new cheating game:
http://www.flashplayer.com/games/theclassroom2.html
It's fun and it's popular, but I could easily create that, it takes very little AS knowledge.
GhostToast
11-09-2004, 09:18 AM
Ok, here is the error im getting:
**Error** Scene=Scene 1, layer=Layer 3, frame=1:Line 3: Statement must appear within on handler
if (Key.isDown(Key.UP)) {
...
GhostToast
11-09-2004, 09:42 AM
Hey, TBC, you made the box of fun thing didn't you? Could you teach me the car stuff?
The Brown Cow
11-09-2004, 03:39 PM
It just comes down to the trig and stuff that Dark posted. I just added some fancy things on top of that.
You error there is because your code is not - as the message says - within a handler.
You can't just put code into a movie clip's code like that. When would it run? You need to tell Flash what event will cause the code to run. You use a handler for this.
Maybe you want it to run when the movie clip is clicked... you can use on(release) or on(press).
But what you want here is for it to run every frame. So, we use onClipEvent(enterFrame).
onClipEvent(enterFrame) {
// Stuff to be run every frame goes here
}
GhostToast
11-09-2004, 05:16 PM
Ok,
I have a file, with one layer, on that layer it the "movie clip" named car, and instance name of car... I put this code for the object:
onClipEvent(enterFrame) {
//When key UP is pressed, speed is increased
if (Key.isDown(Key.UP)) {
speed += 1;
} else {
//When key DOWN is pressed, speed is decreased
if (Key.isDown(Key.DOWN)) {
speed -= 1;
} else {
//If UP or DOWN aren't pressed then the speed decreases
speed *= .9
}
}
//The car will start to slow down after the speed of 25
if (Math.abs(speed)>25) {
speed *= .6;
}
//Turns the car left
if (Key.isDown(Key.LEFT)) {
_rotation -= speed;
}
//Turns the car right
if (Key.isDown(Key.RIGHT)) {
_rotation += speed;
}
//Moves the car
speed *= .9;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.move.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.3;
}
}
It doesn't come up with any errors but it also does not move the car at all.
I dont know why, Only guess is that it has something to do with the object name.
The Brown Cow
11-09-2004, 05:21 PM
Well, you didn't define speed as anything at first, so Flash sees it as undefined. Can't do math with undefined...
undefined - 1 = NaN (Not a Number)
undefined * .06 = NaN
Not good. So you need to define speed as something (probably 0) before you get to doing math on it.
Add this before the rest of that code.
onClipEvent(load) {
speed = 0;
}
That will run when the movie clip is loaded onto the stage by Flash, preparing the variable for future use.
GhostToast
11-09-2004, 05:34 PM
THANK YOU!
lol
You are all so helpful :) Now I can play with this and test for different accelerations ect. But it works! Im so happy lol.
GhostToast
11-09-2004, 06:36 PM
Ok, I added another car called speeder on a second layer, (cause its faster) , both the tank (car1) and speeder (car2) move at the same time.
How would i change it so that if you pressed something like Page Up,
It would switch between the car being controlled?
on (keyPress "<PageUp>") {
//something to select next car from a list
}
The Brown Cow
11-09-2004, 06:48 PM
My approach was first to runn the code from the main timeline, rather than from within a movie clip, but you can probably still do it...
My approach was to have a variable that stored a reference to the current vehicle. When the user entered another vehicle, the variable was switched to contain the new vehicle. The movement code then just runs on the variable, so it controls the current vehicle.
Not gonna give you code on this one, sorry. It's pretty complicated, and I'm kinda protective of my little box of fun. ;D
GhostToast
11-09-2004, 06:49 PM
Lol, your box of fun was great.. but more than i can do, I cant make a person able to walk around and enter cars yet, and i can make stuff colide, or shoot and explode etc...
GhostToast
11-09-2004, 06:53 PM
actually.. I wont ask you how to do all of that if you show me how to do the collison stuff so i can add outside boundrys ... please?
DarkReality
11-10-2004, 02:40 AM
There are a lot of good collision tutorials, but for your sake, something like:
if (car._y > BottomBoundary - car._height) {
car._y = BottomBoundary;
car.speed *= -1 * constant;
}
if (car._y < TopBoundary + car._height) {
car._y = TopBoundary;
car.speed *= -1 * constant;
}
if (car._x> LeftBoundary - car._width) {
car._x = LeftBoundary;
car.speed *= -1 * constant;
}
if (car._x < RightBoundary + car._width) {
car._x = RightBoundary;
car.speed *= -1 * constant;
}
Where constant is a number between 0 and 1. If you pick 1, however, it'll look like the car is purely elastic. Once again, you'll have to see what value works, as I have no idea what the elasticity of a car running against a wall is :-D.
The point of the car._width is to make it so that the car isn't halfway offscreen when it hits a boundary.
As to the car switching problem, you will definately have to move the entire code into the main timeline. That'll require some renaming of variables (such as _x to car._x). There's a tutorial on variable paths on Actionscript.org you might want to look at and one of Kirupa concerning Object Oriented programming which touches on the subject of putting everything into the main timeline, should you not be familiar with it. Now, remember the tidbit:
car._x += car._y += v*Math.cos(car.angle);
car._y += v*Math.sin(car.angle);
?
Say you have A and S assigned to your two vehicles.
if (Key.isDown(Key["A"]){
currentVehicle = car1;
}
if (Key.isDown(Key["S"]){
currentVehicle = car2;
}
Instead of car._x and car._y, you now have "currentVehicle._x" and "currentVehicle._y". You'll also have to add something that'll reset speed and acceleration with each switch, otherwise your cars will immediately begin moving if you switch while the other one is still moving.
Not gonna give you code on this one, sorry. It's pretty complicated, and I'm kinda protective of my little box of fun. ;D
Yay for open source? :-D
The Brown Cow
11-10-2004, 01:53 PM
Yay for open source? :-D
Well, I always document my code, largely for my own sake, but also in case I release it one day.
Maybe when I'm done with Cowpie and Box of Fun. BoF is over 600 lines of code already, which makes me somewhat protective. :)
DarkReality
11-10-2004, 01:59 PM
... 600 lines? Wow. That's quite a bit, I think I might have to check it out again... or at least I was going to until I realized there was no link to it. Do you regularily upload it, or are you waiting til it's all "done"?
The Brown Cow
11-10-2004, 02:02 PM
http://www.browncowinternational.com/temp/vehicles.html
I'm waiting 'til it's done to officially add it to the site. But I'm busy with project Cowpie for now. Once that's done, I'll get back to work on it.
GhostToast
11-10-2004, 05:29 PM
Where do the boundary things go? I cant find anywhere that makes them work...
The Brown Cow
11-10-2004, 05:41 PM
It's not a direct copy and paste operation. TopBoundary, constant, and all those need to be defined. It should go in place of the end of the movement code.
GhostToast
11-10-2004, 06:02 PM
how would i set Stuff like top boundary? is 0,0 the top left?
The Brown Cow
11-10-2004, 06:24 PM
Yes, 0,0 is the top left corner of the Flash movie.
So, the bottom right corner is determined by the dimensions of your movie. By default, it's 550,400.
GhostToast
11-10-2004, 06:40 PM
if (car._y > 400 - car._height) {
car._y = 400;
car.speed *= -1 * 1;
}
if (car._y < 0 + car._height) {
car._y = 0;
car.speed *= -1 * 1;
}
if (car._x> 0 - car._width) {
car._x = 0;
car.speed *= -1 * 1;
}
if (car._x < 600 + car._width) {
car._x = 600;
car.speed *= -1 * 1;
}
That is inside of the code with the on clip event enter frame thing below the movement script.
+If the car has headlight light when it is moving, will that affect when it thinks the car collides?
--Also what is the actionscript to play a wav sound?
The Brown Cow
11-10-2004, 06:47 PM
Headlight will affect the dimensions of the movie clip, so yes.
You play sounds through ActionScript by using the Sound class. First you need to import the sound to the library in the suthoring environment, and choose to export it for actionscript.
Then we create a new Sound object, attach the sound, and play it.
truckHorn = new Sound();
truckHorn.attachSound("truck_horn_sound");
truckHorn.start();
GhostToast
11-10-2004, 06:55 PM
First of all, what is "suthoring environment"
also, would this work? :
if (Key.isDown(Key["H"])) {
carHorn = new Sound();
carHorn.attachSound("vehicle047");
carHorn.start();
}
And, the boundry thing isn't working at all still. 0.o
The Brown Cow
11-10-2004, 07:56 PM
Authoring environment (I type fast, not well). Your copy of Flash, in other words.
For one thing, there's not Key["H"], you have to use the key code that represents the H key. I'm so kind as to have made this handy little tool (http://browncowinternational.com/temp/GETKEY.swf) for getting keycodes. Click the link and press H. You get 72.
So, you have to use
if (Key.isDown(72)) {
// Whatever
}
Thing is, your sound code there will kinda blare the sound continously, because it will start playing it every frame that the key is still down. Dealing will this is not so much fun, and I probably don't have the best way of doing it, so I recommend looking for tutorials on making a toggle key or something of the sort.
It might also be a good idea to create the sound object in the onLoad handler instead of every frame. You might get some weird effects recreating the sound every frame.
At this point, we're gonna need to look at your FLA to figure out the collisions problem.
arkhan
11-11-2004, 05:28 PM
abouyt th headlight..the movie clip dimensions are made atarting on the lil cross.that spot is the 0,0..so, if the headlight is above that and the car below, the colision should only ocour when it reaches the 0 spot, and so, the headlight shall not colide..
DarkReality
11-12-2004, 02:24 PM
You could also easily sidestep around the problem by making the headlights a different object and giving it the same _x and _y position and rotation as the car. Then you just have to play around with how they have to look so that they're in the right place.
GhostToast
11-12-2004, 05:54 PM
Well, the little cross is in the center of the car clip. I dont know how to change it in a movie clip.
GhostToast
11-12-2004, 09:20 PM
Ok about the car switching, would I be able to make a key press thing to change the car variable in the main time line, and then in each car object, just have it all enclosed in an
if ($car==this_cars_variable) {
on load() {
// car stuff
}
}
?
Bodadem
11-12-2004, 09:56 PM
A sample of what you could do with this code, along with some other. code. Ask me if you want to know how to do anything shown here. (note, i know the lines aren't boundries, they're not sposed to be. You can try driving between them if you want though...)
Click Here (http://www.freewebs.com/bodadem/cars.html)
1 and 2 switch to the respective car, arrows move, L turns on/off the headlights.
GhostToast
11-12-2004, 10:12 PM
Umm I would like to know how to switch the car being used like that lol... I have been trying a couple different things that have been posted and my own ideas, but they wont work.
Bodadem
11-12-2004, 10:36 PM
Fairly simple. Have your two cars. Have the movement code in each. Make a variable. (I called mine "currentcar") and add into the movement code for the car that the key must be down, AND the variable must be set for that car.
EX:
onClipEvent (load) {
speed = 0;
}
onClipEvent (enterFrame) {
//When key UP is pressed, speed is increased
if (Key.isDown(Key.UP) && _root.car1.currentcar == 2) {
speed += 1;
} else {
//When key DOWN is pressed, speed is decreased
if (Key.isDown(Key.DOWN) && _root.car1.currentcar == 2) {
speed -= 1;
} else {
//If UP or DOWN aren't pressed then the speed decreases
speed *= .9;
}
}
//The car will start to slow down after the speed of 25
if (Math.abs(speed)>25) {
speed *= .6;
}
//Turns the car left
if (Key.isDown(Key.LEFT) && _root.car1.currentcar == 2) {
_rotation -= speed;
}
//Turns the car right
if (Key.isDown(Key.RIGHT) && _root.car1.currentcar == 2) {
_rotation += speed;
}
//Moves the car
speed *= .9;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.move.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.3;
}
}
The variable shows as "_root.car1.currentcar", because the variable is "currentcar" and i set it in "car1" which is in "_root(main stage)", and it must be set as "2" for this code to work, which is the code for car2.
Now you just have to set somewhere that when the keys are pressed, it changes the variable, hence changing the car you can control.
Hope you understand.
GhostToast
11-12-2004, 10:59 PM
Well Its working, I just have buttons to select each car for now.
GhostToast
11-13-2004, 02:47 AM
An Idea I have to get past the lights problem and add collisions.
My flash has 5 cars now, each named 'car1' - 'car5'
This is the collision code (which mostly works except counts the lights as collision and doesn't have a good crash effect yet)
onClipEvent (enterFrame) {
if (_root.car1, hitTest(_root.car2)) {
_root.text = "Collision Detected";
speed *= -1 * .3;
} else
if (_root.car1, hitTest(_root.car3)) {
_root.text = "Collision Detected";
speed *= -1 * .3;
} else
if (_root.car1, hitTest(_root.car4)) {
_root.text = "Collision Detected";
speed *= -1 * .3;
} else
if (_root.car1, hitTest(_root.car5)) {
_root.text = "Collision Detected";
speed *= -1 * .3;
} else {
_root.text = "No Collision";
}
}
something I am trying is, in each car, set the main body to a movie clip symbol named 'car_#' and in the collision test just check if car_1 hits car_2 etc. so it doesn't check if the lights hit. But it doesn't seems to work.
Last thing is the crash effect.. the cars can force their way through each other and only the car being used is effected.. I want to have the car that gets hit pushed over abit. :roll:
Bodadem
11-13-2004, 08:20 AM
Is the one on your site the most recent? Because i see no collision detection going on there....
GhostToast
11-13-2004, 12:39 PM
The one on my site does not have the collision detection
Bodadem
11-13-2004, 02:28 PM
Ah...Mind uploading the most recent so we can see?
GhostToast
11-14-2004, 01:21 PM
Nope because I forgot to save the one that had a normal collision detection.. its just being all screwy now and fustrating... :(
Im gonna go crazy trying to make this work.
Bodadem
11-14-2004, 02:43 PM
Ah..Okay...Good luck.
GhostToast
11-21-2004, 04:35 PM
ok, the new one is done, AND the lights are ignored with collisions, BUT I dont know why the FTP thing wont upload the new one correctly.
http://www.freepgs.com/ghost_toast/site/flash/
If its up you will notice.. anyways I need a better crash effect! Not sure how to do one.
This is all that happens on the current one:
And it only affects the car being driven.
_root.car1.speed *= -1 * 1;
Bodadem
11-21-2004, 04:58 PM
Your collisions really don't work that well, either..you can force your way through the car pretty easily..I had the same problem though, so i can't help you..
GhostToast
11-21-2004, 05:04 PM
Thats why i need a better crash effect :P
The Brown Cow
11-21-2004, 05:11 PM
I hate collision detection. No fun to make at all, never works as well as you want it to...
The collisions in Box of Fun are done by mathamatically checking for a collision between two imaginary circles around the cars. This is better than Flash's hitTest(), but still far from perfect. Objects that are long in one direction and not very wide work really weird. Hence, I don't have any vehicles that are very long (weak, I know).
Anyway, you do know that -1 * 1 is equal to -1, right? You're just making the computer do an extra calculation every time.
If you want to force the player away from the object they hit a bit more, you can give them a bit of a speed boost. Might look wierd though. Play with the value.
_root.car1.speed *= -1.2;
crusty
11-21-2004, 06:42 PM
I'm kind of jumping in here from nowhere, but, when you make collision detection, are you drawing a boundary, or does the program know where the edges of the layer are?
If you draw a boundary, then you might want to make it a little closer to the edge of the car. I'm about a millimeter away from a car, and it says "Collision Detected."
Nexus
11-21-2004, 08:03 PM
Flash does not "know" where the boundary line is, if that's what your question was.
Flash has a few handly pieces of info like Stage.width and Stage.height, which get the "boundaries" of the stage I guess you could say. But the actual collision detection is all mathematical. Computers are stupid. They may be fast at performing mathematical calculations, but that's it. A computer does not think "outside" of the box. It's world is the mathematical world.
It's up to the programmer to create software that can, using nothing other than mathematical algorithms, work out problems people face in everyday life.
That's my little spiel. I'm back baby.
crusty
11-22-2004, 10:48 AM
I'm crusty, if you remember me.
Long time no see.
I want to get back into Flash, but I can't find anyone who'll send me a copy. I don't feel like paying $500+ for it, either.
:|
Bodadem
11-22-2004, 02:34 PM
OOH OOH!!! Pirate it.
Nexus
11-22-2004, 03:01 PM
No, don't pirate it.
Tempting as it may be, it can only lead to bad things. In the end it's worth it to see that little box sitting there, knowing you earned it.
$500 if you think about it isn't hard to come by. I've got friends who only work one or two days a week and still pull off a paycheck of $300 a month. (granted, you don't find jobs like that very easily :))
I don't really know what else to say, just don't pirate!
The Brown Cow
11-22-2004, 03:36 PM
Academic version!
I got the entire Macromedia studio for $400. My friend managed to get it for $200. It normally costs $1000, and this way is still legal (assuming you go to some form of school).
crusty
11-22-2004, 04:00 PM
I managed to crash your sig, Nexus.
:P
Bodadem
11-22-2004, 07:56 PM
And yet, I still say pirate it. I did, and I have no guilty feelings. They make enough damn money without jacking $500 off me!
arkhan
11-23-2004, 08:53 AM
keep in mind that lil thingy
this forums
no :D
ilegal activity
maan..so long!!
whatever you did or didnt pirate it, this forums aint the place to put up a discussion about it, because whatever you get away with no heart fellings, you are still the wrong one..
this boards are meant to learn flash, and not help you on illegal activities..
altought i cant say much, coz Im still using my bad version until I get to work next year and buy the real version(possibly the ball8 one :D )
DarkReality
11-23-2004, 09:21 AM
So you're telling him not to use a pirated version, yet are using one yourself for the same reason he is (lack of money)? They used to burn people like you at the stake. Buuuuurn them, I tell you. And oh how they buuuuuurned. Lit the whole yard, I tell you. They lit the whole yard with their fiery aura.
Not really, but it's still a bit hypocritical.
Then again, it's Macromedia's own fault. If they make it that easy to simply generate a Serial Number and unlock the demo to turn into a full blown, 500 dollar program, then they have it coming their way. They could simply offer a "demo" Flash studio, which simply doesn't allow you to export anything. And by "demo" I mean one that you can't unlock. I literally mean a demo.
arkhan
11-23-2004, 11:26 AM
or simply like maya...put a watermark on everything...
and notice I didnt say "dont use"..I simply said this isnt the place to discuss this..
Nexus
11-23-2004, 02:02 PM
So you're telling him not to use a pirated version, yet are using one yourself for the same reason he is (lack of money)? They used to burn people like you at the stake. Buuuuurn them, I tell you. And oh how they buuuuuurned. Lit the whole yard, I tell you. They lit the whole yard with their fiery aura.
I thought they still did that. :shock:
I don't want to be burninated.
Bodadem
11-24-2004, 06:09 PM
You're still being hypocritical by telling us not to discuss it, when you yourself are joining in the discussion by telling us not to discuss it when we were discussing a perfectly discussable discussion untill you discussed our discussion!
That word is starting to ont look like a word..ever notice that? Say (or write) a word a whole lot, and you question the existance of the word because it just sounds so silly?
vBulletin® v3.7.3, Copyright ©2000-2010, Jelsoft Enterprises Ltd.