View Full Version : Help writing Actionscript for game
sumei
11-13-2001, 10:47 AM
Hi There
I hope somebody can lend me thier expertise. I need to make a game, It's simple in concept, but I'm struggling to put the action and variables and random maths, together.
The game is chopping kings and queens heads off, it's for a history site.
So you have 5 different heads, popping up behind 5 chopping boards at random and using the ax attached to the mouse._x._Y
you got to hit them, and hence earn points.
But I can't seem to get the heads to pop up at random intervals or appear at random chopping boards.
Your Help will be greatly appreciated.
Ryan
wangbar
11-13-2001, 01:57 PM
Try this, maybe it will help...
Set up your five "head" movieclips with instance names "head1" through "head5", then add this script to a dummy movieclip on the main timeline-
onClipEvent (load) {
// sets random time based on current value of getTimer()
function setRandomTime () {
now = getTimer();
delay = Math.floor(Math.random()*10000);
popTime = now+delay;
return popTime;
}
// initialise head positions and set random pop up times
for (i=1; i<=5; i++) {
head = _root["head"+i];
head._y = 200;
head.upTime = setRandomTime();
}
}
onClipEvent (enterFrame) {
for (i=1; i<=5; i++) {
head = _root["head"+i];
// pop up head
if (head.upTime<getTimer() && head.upTime+3000>getTimer()) {
head._y = 100;
} else {
// put head down again and calculate next pop up time
head._y = 200;
head.upTime = setRandomTime();
}
}
}
mad_A
11-13-2001, 02:02 PM
OK, say you have 5 heads, 10 locations.
Heads called H1, H2, H3...
Locations called L1, L2, L3,...
numHeads=5;
numLocs=10;
function showhead(){
// get random head
head = random(5)+1;
head="_root.H" add head;
// get random location
location=random(10)+1;
// get location for the head from the location
locationX = Eval("L" add location add "._x";
locationY = Eval("L" add location add "._y";
// set the position for the head
setProperty(head, _x, locationX);
setProperty(head, _y, locationY);
Eval(head add "._visible")=true;
}
you can clean up the code a bit, but that'll do it for ye
sumei
11-14-2001, 09:14 AM
I'd really like to thank both of you for you speedy replies.
Mad A I couldn't get yours to work, I pasted the script on Frame2 with a gotoAndPlay (1) on frame 3, I put the variables on frame1.
but had no luck. I couldn't get my head round why the location variables were needed, along with the location _x, when all I want is the _y value.
This is the error in output
Scene=Scene 1, Layer=Layer 2, Frame=2: Line 8: ')' or ',' expected
locationX = Eval("L" add location add "._x";
Scene=Scene 1, Layer=Layer 2, Frame=2: Line 9: ')' or ',' expected
locationY = Eval("L" add location add "._y";
if you know what I could have done wrong, which is probably not realistic, help would be appreciated.
sumei
11-14-2001, 10:09 AM
I'd really like to thank both of you for you speedy replies.
Mad A I couldn't get yours to work, I pasted the script on Frame2 with a gotoAndPlay (1) on frame 3, I put the variables on frame1.
but had no luck. I couldn't get my head round why the location variables were needed, along with the location _x, when all I want is the _y value.
This is the error in output
Scene=Scene 1, Layer=Layer 2, Frame=2: Line 8: ')' or ',' expected
locationX = Eval("L" add location add "._x";
Scene=Scene 1, Layer=Layer 2, Frame=2: Line 9: ')' or ',' expected
locationY = Eval("L" add location add "._y";
if you know what I could have done wrong, which is probably not realistic, help would be appreciated.
the error you are getting from mad_a's code is cause there is no closing paran on the eval statement... put one of these ")"right before the semi-colon (on the code for both the location X and location Y ).
evilregis
11-14-2001, 02:45 PM
you are simply missing the closing bracket ')' at the end of the eval statement. add that into both lines and you should lose those errors.
.:er:.
mad_A
11-16-2001, 10:41 AM
Opps!
Sorry about that. As the lads said - I left out the closing brackets...
....had to leave you something to do!;)
mad_A
11-16-2001, 10:45 AM
gee...my code iks getting worse and worse...another error, the value for random should be one less as I am adding one to it (BECAUSE RANDOM INCLUDES ZERO)..so here is the modified code:
function showhead(){
// get random head
head = random(4)+1;
head="_root.H" add head;
// get random location
location=random(9)+1;
// get location for the head from the location variable
locationX = Eval("L" add location add "._x");
locationY = Eval("L" add location add "._y");
// set the position for the head
setProperty(head, _x, locationX);
setProperty(head, _y, locationY);
Eval(head add "._visible")=true;
}
wangbar
11-16-2001, 11:52 AM
Yeah, I had a look through my code too and made a couple of tweaks.
Here's the update:
onClipEvent (load) {
// sets random time based on current value of getTimer()
function setRandomTime () {
now = getTimer();
delay = Math.floor(Math.random()*10000);
popTime = now+delay;
return popTime;
}
// initialise head positions and set random pop up times
upPosition = 120;
downPosition = 200;
for (i=1; i<=5; i++) {
head = _root["head"+i];
head._y = downPosition;
head.upTime = setRandomTime();
}
}
onClipEvent (enterFrame) {
for (i=1; i<=5; i++) {
head = _root["head"+i];
// pop up head
if (head.upTime<getTimer() && head.upTime+2000>getTimer()) {
head._y -= (head._y-upPosition)/2;
} else {
// put head down again and calculate next pop up time
head._y += (downPosition-head._y)/2;
head.upTime = setRandomTime();
}
}
}
I'm sure that if you combine this and Mad A's together you'll have something that works the way you want, in the meantime I put a demo of the above up at:
chopping demo (http://www.wangbar.co.uk/chopping.swf)
|
vBulletin® v3.8.5, Copyright ©2000-2013, Jelsoft Enterprises Ltd.