PDA

View Full Version : [AS2] Shooting game help


ritard200
12-11-2010, 11:02 PM
im trying to make a shooting game in which a character runs around and shoots, so i use this script
var reloadSpeed:Number = 250;
var shotOffset:Number = 10;
var shotWidth:Number = 2;
var shotColor = "0xFFFF00";
var rad:Number = Math.PI/180;
var rad2:Number = 180/Math.PI;

var reloadTimer:Number;
var screenTimer:Number;
var reloadComplete:Boolean = true;

function fireWeapon():Void
{
if(reloadComplete == true)
{
endX = _root._xmouse + randomNumX
endY = _root._ymouse + randomNumY
randomNumX = random(shotOffset)-(shotOffset/2);
randomNumY = random(shotOffset)-(shotOffset/2);
_root.lineStyle(shotWidth, shotColor, 100);
_root.moveTo(char._x, char._y);
_root.lineTo(_root._xmouse + randomNumX, _root._ymouse + randomNumY);
startReloading();
}
}
function startReloading()
{
reloadComplete = false;
reloadTimer = setInterval(this, "gunReloaded", reloadSpeed);
screenTimer= setInterval(this,"clearScreen", 100);
}
function clearScreen()
{
clearInterval(screenTimer);
_root.clear();
}
function gunReloaded()
{
clearInterval(reloadTimer);
reloadComplete = true;
}

mouseListener2 = new Object();

mouseListener2.onMouseDown = function() {
fireWeapon();
}

Mouse.addListener(mouseListener2)

_root.onEnterFrame = function() {
fireWeapon();
}


for the shooting.
unfortunatly, the bullet ends up ending where my pointer is, totally unreallistically, anyone know how to make the bullet go farther than the mouse? im assuming it has to do with _root.lineTo(_root._xmouse + randomNumX, _root._ymouse + randomNumY);

ritard200
12-13-2010, 01:14 AM
I'm trying to make a script where you shoot, and i cant get the shooting done, and have it detect hitting a wall. I think it has something to do with the fireWeapon, the movement and rotation of the character is done on the characters movie symbol itself

var maxDistance:Number = 500
var accuracy:Number = 1

var reloadSpeed:Number = 250;
var shotOffset:Number = 10;
var shotWidth:Number = 2;
var shotColor = "0xFFFF00";
var rad:Number = Math.PI/180;
var rad2:Number = 180/Math.PI;
var realDist:Number;
map_mc.createEmptyMovieClip("shot_mc", map_mc.getNextHighestDepth());
var dx:Number;
var dy:Number;
var startX:Number;
var startY:Number;
var endX:Number;
var endY:Number;


var reloadTimer:Number;
var screenTimer:Number;
var reloadComplete:Boolean = true;


mouseListener2 = new Object();

mouseListener2.onMouseDown = function() {
fireWeapon();
}

Mouse.addListener(mouseListener2)


function fireWeapon():Void
{
if (reloadComplete == true)
{
_root.onEnterFrame = function ():Void {
updateShot();
startReloading();
}
}
}
function updateShot() :Void
{
for (reaDist=0; maxDist<realDist; realDist += accuracy)
{
endX = char._x + Math.cos(char._rotation * rad) * realDist;
endY = char._y + Math.sin(char._rotation * rad) * realDist;
if (map_mc.hitTest(endX,endY,true)){
break;
}
}
startX = char._x + 15 * Math.cos(char._rotation * rad);
startY = char._y + 15 * Math.sin(char._rotation * rad);
shot_mc.clear();
shot_mc.lineStyle(shotWidth, shotColor, 100);
shot_mc.moveTo(startX, startY);
shot_mc.lineTo(endX, endY);
}

function startReloading()
{
reloadComplete = false;
reloadTimer = setInterval(this, "gunReloaded", reloadSpeed);
screenTimer= setInterval(this,"clearScreen", 100);
}
function clearScreen()
{
clearInterval(screenTimer);
delete _root.onEnterFrame;
shot_mc.clear();
}
function gunReloaded()
{
clearInterval(reloadTimer);
reloadComplete = true;
}