View Full Version : Random Terrain & Hit Test
Pneumonic
November 30th, 2004, 12:54 AM
Here's the sitiation. I'm making a pocket tanks-esque game, but I'll be using cannons and you cannot move. Here's the deal:
2 Questions:
1st) Using 1x1 pixels how can I generate random terrains with a top 1x1 pixel black border?
2nd) How can I make these 1x1 pixels disappear when the cannonball explodes in a certain area? (i.e. Pseudo code)
onhit.Test(cannonball && ground)
playmovie(explosion) at cannonball._x && cannonball._y
remove.this.cannonball.MovieClip
If hit.Test == True
Remove movie clip(ground) where
explosion == ground._x && ground._y
I realize my coding is really bad and not in syntax, but I was just trying to get the gist of it across as I was writing it down in my notebook earlier.
If I'm not able to make a random terrain, it would still be appreciated if I could make a terrain that was always the same, but using those small 1x1 pixel mc's, but I don't know how I'd do that. Would I just use a duplicate for, command to populate a boxed in area? And how do I define the boxed in area?
Thanks for the help in advance
Dr Warm
November 30th, 2004, 09:08 AM
It's difficult to make a game like this in flash, because it's very CPU intensive, but i have got a couple of flas i'll post here that should help you, I just have to find them!!
I'll try and find them tomorrow, as i'm getting tired now, and i haven't ordered them very well!
BTW 400th POST!!!!!!!!!!
Pneumonic
November 30th, 2004, 02:18 PM
congrats on the 400 and thanks for the help. I appreciate it. I'm not too sure abot the boundary thing in my other post. I think I'll leave that alone for now, because I have too much other stuff to do. This is beginning to be frustrating.
Also, if this would be better done in something like C++, I have visual studio (and the C++ language for it), so I can do it in this as well. I just wanted to use flash, because I thought it would be the easiest. I'm sure porting the code wouldn't be too hard, I'd just have to make all the other stuff to contain it, as well.
Pneumonic
November 30th, 2004, 05:18 PM
Ok, I think I've figured out what I should do rather than what I was attempting to do.
I would create a few terrains using flash or illustrator, and randomize those terrains using an array, rather than dynamically creating an entire one. My question for that is, when I attempt to do a hit.Test would it hit the mc's box, or the actual lines?
Then, when the cannonball hits the ground, it would play an explosion, and then where the explosion occurred it would mask that section out. (and possibly subtract that part out of the ground variable so things could fall.)
I hope this is easier to do.
Here's both the codes I'm trying to use.
cannon.onEnterFrame = function(){
if(Key.isDown(Key.RIGHT)){
cannon._rotation +=1;
angle.text = cannon._rotation;
}
if(Key.isDown(Key.LEFT)){
cannon._rotation -= 1;
angle.text = cannon._rotation;
}
if(Key.isDown(Key.UP)){
cannon.vel += 1;
power.text = cannon.vel;
}
if(Key.isDown(Key.DOWN)){
cannon.vel -= 1;
power.text = cannon.vel;
}
}
function DistanceBetween(cannon, target){
var dx = target._x - cannon._x;
var dy = target._y - cannon._y;
return Math.sqrt(dx*dx+dy*dy);
}
function RotationFromTo(cannon, target){
var vector_x = target._x - cannon._x;
var vector_y = target._y - cannon._y;
return RotationFromVectors(vector_x, verctor_y);
}
function AngleFromRotation(mc){
return mc._rotation * Math.PI/180;
}
function RotationFromVectors(x,y){
return Math.atan2(y,x)*180/Math.PI;
}
cannon_length = 30
cannon.vel = 10
friction = 3;
gravity = 1;
depth = 0;
cannon_bounds={xMin:10, xMax:200, yMin:150, yMax:150}
cb_bounds={xMin:-50, xMax:650, yMin:-300, yMax:ground._y}
target_bounds={xMin:300, xMax:580, yMin:20, yMax:ground._y}
function cannonballEnterFrame(){
this.vector_y += gravity;
this._x += this.vector_x;
this._y += this.vector_y;
if(this._y>=ground._y){
this._y = ground._y;
this.vector_y = -Math.abs(this.vector_y);
}
this.rotation = RotationFromVectors(this.vector_x, thix.vector_y);
if(target.hitTest(this._x, this._y, true)){
hit.text = "TARGET HIT!";
}
if(this._xcb_bounds.xMax)
this.removeMovieClip();
else if(this._ycb_bounds.yMax)
this.removeMovieClip();
}
fire.onRelease = function(){
var curr_cb = this.attachMovie("cb","cb"+depth+"",depth);
var cannon_angle = AngleFromRotation(cannon);
var direction_x = Math.cos(cannon_angle);
var direction_y = Math.sin(cannon_angle);
curr_cb._x = cannon._x + direction_x * cannon_length;
curr_cb._y = cannon._y + direction_y * cannon_length;
curr_cb.vector_x = direction_x * cannon.vel/friction;
curr_cb.vector_y = direction_y * cannon.vel/friction + gravity;
curr_cb.onEnterFrame = cannonballEnterFrame;
depth++;
}
angle.text = cannon._rotation;
power.text = cannon.vel;
and here's the fla, for this one...http://www.21bluefish.com/media_in_progress/fla's/cannon.fla
and the code for the second one
init();
function init(){
cannon.vel = 10;
}//end init
cannon.onEnterFrame = function(){
//this will happen each frame rotating the
//cannon left and right with the arrow keys
if(Key.isDown(Key.RIGHT)){
cannon._rotation +=1;
angle.text = cannon._rotation;//this updates the angle text field
}//end if
if(Key.isDown(Key.LEFT)){
cannon._rotation -= 1;
angle.text = cannon._rotation;//this updates the angle text field
}//end if
if(Key.isDown(Key.UP)){
cannon.vel += 1;
power.text = cannon.vel;//this updates the power text field
}//end if
if(Key.isDown(Key.DOWN)){
cannon.vel -= 1;
power.text = cannon.vel;//this updates the power text field
}//end if
//this attaches the onRelease to the button
//without attaching it to the button directly
fire.onRelease = function(){
//make a cannonball
_root.attachMovie("cb","cb",10);
cb._x = cannon._x;
cb._y = cannon._y;
//offset by 90 degrees
tempAngle = cannon._rotation - 90;
//convert to radians
tempAngle = tempAngle / 180 * Math.PI;
//get DX and DY(normalized: length is one)
cb.dx = Math.cos(tempAngle) * cannon.vel;
cb.dy = Math.sin(tempAngle) * cannon.vel;
cb.onEnterFrame = function(){
cb._x += cb.dx;
cb._y += cb.dy;
cb.dy += 1;
}//end enterFrame
}//end if
}//end enterFrame
angle.text = cannon._rotation;
power.text = cannon.vel;
and the fla for this one is at http://www.21bluefish.com/media_in_progress/fla's/cannon2.fla
Something is coded wrong with the first one, as it doesn't fire, but the second one doesn't look realistic at all, so I'm unsure of where to go from here.
Thanks for the help.
Dr Warm
November 30th, 2004, 11:23 PM
ok i think this fla should suit you're needs, it even has the removeMovieClip working!
i can't remember where i found this but kudos to the person who made it!
Pneumonic
December 1st, 2004, 01:00 AM
Thanks for that...I have another question that deals with somewhat the same subject for a different project.
I'm going to have a ball and it's going to be shot at another object, but the objects are all in a row, so it's possible to hit multiple objects in real life, but can that be simulated in flash? would I make several movie clips all with different registration points and throw them all into one main ball movie clip that would then have all the registration points?
Thanks for the help.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.