00:00
00:00
Newgrounds Background Image Theme

LewgusWithFriends just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

as3 Hit test for enemy

630 Views | 4 Replies
New Topic Respond to this Topic

checking if bullet is hitting the enemy or player in the code
i've tried a couple thing but more errors pop up when i fix one

code with not working hittest function removed:

import flash.display.MovieClip;
import flash.geom.Point;
import flash.events.Event;
import flash.events.MouseEvent;
//these booleans will check which keys are down
var leftDown: Boolean = false;
var upDown: Boolean = false;
var rightDown: Boolean = false;
var downDown: Boolean = false;
//how fast the character will be able to go
var mainSpeed: int = 5;
var mcMain: MovieClip;
Main();
Main();
//adding a listener to mcMain that will move the character
mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event: Event): void {
//checking if the key booleans are true then moving
//the character based on the keys
if (leftDown) {
mcMain.x -= mainSpeed;
}
if (upDown) {
mcMain.y -= mainSpeed;
}
if (rightDown) {
mcMain.x += mainSpeed;
}
if (downDown) {
mcMain.y += mainSpeed;
}
//keeping the main character within bounds
if (mcMain.x <= 0) {
mcMain.x += mainSpeed;
}
if (mcMain.y <= 0) {
mcMain.y += mainSpeed;
}
if (mcMain.x >= stage.stageWidth - mcMain.width) {
mcMain.x -= mainSpeed;
}
if (mcMain.y >= stage.stageHeight - mcMain.height) {
mcMain.y -= mainSpeed;
}
}
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event: KeyboardEvent): void {
//making the booleans true based on the keycode
//WASD Keys or arrow keys
if (event.keyCode == 37 || event.keyCode == 65) {
leftDown = true;
}
if (event.keyCode == 38 || event.keyCode == 87) {
upDown = true;
}
if (event.keyCode == 39 || event.keyCode == 68) {
rightDown = true;
}
if (event.keyCode == 40 || event.keyCode == 83) {
downDown = true;
}
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event: KeyboardEvent): void {
//making the booleans false based on the keycode
if (event.keyCode == 37 || event.keyCode == 65) {
leftDown = false;
}
if (event.keyCode == 38 || event.keyCode == 87) {
upDown = false;
}
if (event.keyCode == 39 || event.keyCode == 68) {
rightDown = false;
}
if (event.keyCode == 40 || event.keyCode == 83) {
downDown = false;
}
}

// Code by Benoit Freslon.
// Tutorials, Flash games:
// http://www.benoitfreslon.com

// This object will always look at the mouse cursor
mcMain.addEventListener(Event.ENTER_FRAME, tankEnterFrame);
// This function will be launched every frame (25 times by seconds);
function tankEnterFrame(pEvt) {
// pEvt.currentTarget: myTank
var mc = pEvt.currentTarget;
// Get the radian angle between the tank and the cursor
// You can also replace mouseX and mouseY by another coordinates
var angleRadian = Math.atan2(mouseY - mc.y, mouseX - mc.x);
// Convert the radian angle in dedree angle
var angleDegree = angleRadian * 180 / Math.PI;
// Set the orientation
mc.rotation = angleDegree;

}

// Add a mouse down event on stage
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);

function mouseDown(pEvent) {
// Create a new bullet
var b = new Bullet();
// Set his position to the tank position
b.x = mcMain.x;
b.y = mcMain.y;
// Save the randian angle between the mouse and the tank
// This angle will set the direction of the bullet
b.angleRadian = Math.atan2(mouseY - mcMain.y, mouseX - mcMain.x);
// Add an enter frame event on each bullet
b.addEventListener(Event.ENTER_FRAME, bulletEnterFrame);
// Add this display object on the display list
addChild(b);
}

// Velocity of each bullet
var speed = 8;

function bulletEnterFrame(pEvent) {
// Get the current object (Bullet)
var b = pEvent.currentTarget;
// Move this bullet on each frames
// On X axis use the cosinus angle
b.x += Math.cos(b.angleRadian) * speed;
// On Y axis use the sinus angle
b.y += Math.sin(b.angleRadian) * speed;
// Orient the bullet to the direction
b.rotation = b.angleRadian * 180 / Math.PI;
// You have to remove each created bullet
// So after every moves you must check bullet position
// If the bullet is out of the screen
if (b.x < 0 || b.x > 550 || b.y < 0 || b.y > 400) {
// Remove it from the display list
removeChild(b);
// /!\ AND REOMOVE HIS EVENT LISTER
b.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
}

}

// player settings
var _moveSpeedMax: Number = 1000;
var _rotateSpeedMax: Number = 15;
var _decay: Number = .98;
var _destinationX: int = 150;
var _destinationY: int = 150;

var _minX: Number = 0;
var _minY: Number = 0;
var _maxX: Number = 550;
var _maxY: Number = 400;

// player
var _player: MovieClip
// global
var _dx: Number = 0;
var _dy: Number = 0;

var _vx: Number = 0;
var _vy: Number = 0;

var _trueRotation: Number = 0;

/**
* Constructor
*/
function Main() {
// create player object
createPlayer();

// add listeners
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}

/**
* Creates player
*/
function createPlayer(): void {
_player = new Player();
_player.x = stage.stageWidth / 2;
_player.y = stage.stageHeight / 2;
stage.addChild(_player);
}

/**
* EnterFrame Handlers
*/
function enterFrameHandler(event: Event): void {
updateCollision();
updatePosition();
updateRotation();
}

/**
* Calculate Rotation
*/
function updateRotation(): void {
// calculate rotation
_dx = _player.x - _destinationX;
_dy = _player.y - _destinationY;

// which way to rotate
var rotateTo: Number = getDegrees(getRadians(_dx, _dy));

// keep rotation positive, between 0 and 360 degrees
if (rotateTo > _player.rotation + 180) rotateTo -= 360;
if (rotateTo < _player.rotation - 180) rotateTo += 360;

// ease rotation
_trueRotation = (rotateTo - _player.rotation) / _rotateSpeedMax;

// update rotation
_player.rotation += _trueRotation;
}

/**
* Calculate Position
*/
function updatePosition(): void {
// update velocity
_vx += (_destinationX - _player.x) / _moveSpeedMax;
_vy += (_destinationY - _player.y) / _moveSpeedMax;

// if close to target
if (getDistance(_dx, _dy) < 50) {
getRandomDestination();
}

// apply decay (drag)
_vx *= _decay;
_vy *= _decay;

// update position
_player.x += _vx;
_player.y += _vy;
}

/**
* updateCollision
*/
function updateCollision(): void {
// Check X
// Check if hit top
if (((_player.x - _player.width / 2) < _minX) && (_vx < 0)) {
_vx = -_vx;
}
// Check if hit bottom
if ((_player.x + _player.width / 2) > _maxX && (_vx > 0)) {
_vx = -_vx;
}

// Check Y
// Check if hit left side
if (((_player.y - _player.height / 2) < _minY) && (_vy < 0)) {
_vy = -_vy
}
// Check if hit right side
if (((_player.y + _player.height / 2) > _maxY) && (_vy > 0)) {
_vy = -_vy;
}
}

/**
* Calculates a random destination based on stage size
*/
function getRandomDestination(): void {
_destinationX = Math.random() * (_maxX - _player.width) + _player.width / 2;
_destinationY = Math.random() * (_maxY - _player.height) + _player.height / 2;
}

/**
* Get distance
* @param delta_x
* @param delta_y
* @return
*/
function getDistance(delta_x: Number, delta_y: Number): Number {
return Math.sqrt((delta_x * delta_x) + (delta_y * delta_y));
}

/**
* Get radians
* @param delta_x
* @param delta_y
* @return
*/
function getRadians(delta_x: Number, delta_y: Number): Number {
var r: Number = Math.atan2(delta_y, delta_x);

if (delta_y < 0) {
r += (2 * Math.PI);
}
return r;
}

/**
* Get degrees
* @param radians
* @return
*/
function getDegrees(radians: Number): Number {
return Math.floor(radians / (Math.PI / 180));
}

Response to as3 Hit test for enemy 2016-05-04 08:24:18 (edited 2016-05-04 08:25:40)


Please put your code in [code][/code] (replace square brackets with the angle braces <>) tags. Also, what am I supposed to be seeing again? Why is the hittest function removed? What is the hittest function, actually? Is it a simple check or something else?


Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp

"Sit look rub panda" - Alan Davies

BBS Signature

Response to as3 Hit test for enemy 2016-05-04 12:32:55


import flash.display.MovieClip;
import flash.geom.Point;
import flash.events.Event;
import flash.events.MouseEvent;
//these booleans will check which keys are down
var leftDown: Boolean = false;
var upDown: Boolean = false;
var rightDown: Boolean = false;
var downDown: Boolean = false;
//how fast the character will be able to go
var mainSpeed: int = 5;
var mcMain: MovieClip;
Main();
//adding a listener to mcMain that will move the character
mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event: Event): void {
	//checking if the key booleans are true then moving
	//the character based on the keys
	if (leftDown) {
		mcMain.x -= mainSpeed;
	}
	if (upDown) {
		mcMain.y -= mainSpeed;
	}
	if (rightDown) {
		mcMain.x += mainSpeed;
	}
	if (downDown) {
		mcMain.y += mainSpeed;
	}
	//keeping the main character within bounds
	if (mcMain.x <= 0) {
		mcMain.x += mainSpeed;
	}
	if (mcMain.y <= 0) {
		mcMain.y += mainSpeed;
	}
	if (mcMain.x >= stage.stageWidth) {
		mcMain.x -= mainSpeed;
	}
	if (mcMain.y >= stage.stageHeight) {
		mcMain.y -= mainSpeed;
	}
}
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event: KeyboardEvent): void {
	//making the booleans true based on the keycode
	//WASD Keys or arrow keys
	if (event.keyCode == 37 || event.keyCode == 65) {
		leftDown = true;
	}
	if (event.keyCode == 38 || event.keyCode == 87) {
		upDown = true;
	}
	if (event.keyCode == 39 || event.keyCode == 68) {
		rightDown = true;
	}
	if (event.keyCode == 40 || event.keyCode == 83) {
		downDown = true;
	}
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event: KeyboardEvent): void {
	//making the booleans false based on the keycode
	if (event.keyCode == 37 || event.keyCode == 65) {
		leftDown = false;
	}
	if (event.keyCode == 38 || event.keyCode == 87) {
		upDown = false;
	}
	if (event.keyCode == 39 || event.keyCode == 68) {
		rightDown = false;
	}
	if (event.keyCode == 40 || event.keyCode == 83) {
		downDown = false;
	}
}


// Code by Benoit Freslon.
// Tutorials, Flash games:
// http://www.benoitfreslon.com

// This object will always look at the mouse cursor
mcMain.addEventListener(Event.ENTER_FRAME, tankEnterFrame);
// This function will be launched every frame (25 times by seconds);
function tankEnterFrame(pEvt) {
	// pEvt.currentTarget: myTank
	var mc = pEvt.currentTarget;
	// Get the radian angle between the tank and the cursor
	// You can also replace mouseX and mouseY by another coordinates
	var angleRadian = Math.atan2(mouseY - mc.y, mouseX - mc.x);
	// Convert the radian angle in dedree angle
	var angleDegree = angleRadian * 180 / Math.PI;
	// Set the orientation
	mc.rotation = angleDegree;

}

// Add a mouse down event on stage
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);

function mouseDown(pEvent) {
	// Create a new bullet
	var b = new Bullet();
	// Set his position to the tank position
	b.x = mcMain.x;
	b.y = mcMain.y;
	// Save the randian angle between the mouse and the tank
	// This angle will set the direction of the bullet
	b.angleRadian = Math.atan2(mouseY - mcMain.y, mouseX - mcMain.x);
	// Add an enter frame event on each bullet
	b.addEventListener(Event.ENTER_FRAME, bulletEnterFrame);
	// Add this display object on the display list
	addChild(b);
}

// Velocity of each bullet
var speed = 8;

function bulletEnterFrame(pEvent) {
	// Get the current object (Bullet)
	var b = pEvent.currentTarget;
	// Move this bullet on each frames
	// On X axis use the cosinus angle
	b.x += Math.cos(b.angleRadian) * speed;
	// On Y axis use the sinus angle
	b.y += Math.sin(b.angleRadian) * speed;
	// Orient the bullet to the direction
	b.rotation = b.angleRadian * 180 / Math.PI;
	// You have to remove each created bullet
	// So after every moves you must check bullet position
	// If the bullet is out of the screen
	if (b.x < 0 || b.x > 550 || b.y < 0 || b.y > 400) {
		// Remove it from the display list
		removeChild(b);
		// /!\ AND REOMOVE HIS EVENT LISTER
		b.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame);
	}
	if (b.hitTestObject(_player)) {
		removeChild(_player)
	} 

}




// player settings		
var _moveSpeedMax: Number = 1000;
var _rotateSpeedMax: Number = 15;
var _decay: Number = .98;
var _destinationX: int = 150;
var _destinationY: int = 150;

var _minX: Number = 0;
var _minY: Number = 0;
var _maxX: Number = 550;
var _maxY: Number = 400;

// player
var _player: MovieClip
// global		
var _dx: Number = 0;
var _dy: Number = 0;

var _vx: Number = 0;
var _vy: Number = 0;

var _trueRotation: Number = 0;

/**
 * Constructor
 */
function Main() {
	// create player object
	createPlayer();

	// add listeners
	stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}

/**
 * Creates player
 */
function createPlayer(): void {
	_player = new Player();
	_player.x = stage.stageWidth / 2;
	_player.y = stage.stageHeight / 2;
	stage.addChild(_player);
}

/**
 * EnterFrame Handlers
 */
function enterFrameHandler(event: Event): void {
	updateCollision();
	updatePosition();
	updateRotation();
}



/**
 * Calculate Rotation
 */
function updateRotation(): void {
	// calculate rotation
	_dx = _player.x - _destinationX;
	_dy = _player.y - _destinationY;

	// which way to rotate
	var rotateTo: Number = getDegrees(getRadians(_dx, _dy));

	// keep rotation positive, between 0 and 360 degrees
	if (rotateTo > _player.rotation + 180) rotateTo -= 360;
	if (rotateTo < _player.rotation - 180) rotateTo += 360;

	// ease rotation
	_trueRotation = (rotateTo - _player.rotation) / _rotateSpeedMax;

	// update rotation
	_player.rotation += _trueRotation;
}

/**
 * Calculate Position
 */
function updatePosition(): void {
	// update velocity
	_vx += (_destinationX - _player.x) / _moveSpeedMax;
	_vy += (_destinationY - _player.y) / _moveSpeedMax;

	// if close to target
	if (getDistance(_dx, _dy) < 50) {
		getRandomDestination();
	}

	// apply decay (drag)
	_vx *= _decay;
	_vy *= _decay;

	// update position
	_player.x += _vx;
	_player.y += _vy;
}

/**
 * updateCollision
 */
function updateCollision(): void {
	// Check X
	// Check if hit top
	if (((_player.x - _player.width / 2) < _minX) && (_vx < 0)) {
		_vx = -_vx;
	}
	// Check if hit bottom
	if ((_player.x + _player.width / 2) > _maxX && (_vx > 0)) {
		_vx = -_vx;
	}

	// Check Y
	// Check if hit left side
	if (((_player.y - _player.height / 2) < _minY) && (_vy < 0)) {
		_vy = -_vy
	}
	// Check if hit right side
	if (((_player.y + _player.height / 2) > _maxY) && (_vy > 0)) {
		_vy = -_vy;
	}
}

/**
 * Calculates a random destination based on stage size
 */
function getRandomDestination(): void {
	_destinationX = Math.random() * (_maxX - _player.width) + _player.width / 2;
	_destinationY = Math.random() * (_maxY - _player.height) + _player.height / 2;
}

/**
 * Get distance
 * @param	delta_x
 * @param	delta_y
 * @return
 */
function getDistance(delta_x: Number, delta_y: Number): Number {
	return Math.sqrt((delta_x * delta_x) + (delta_y * delta_y));
}

/**
 * Get radians
 * @param	delta_x
 * @param	delta_y
 * @return
 */
function getRadians(delta_x: Number, delta_y: Number): Number {
	var r: Number = Math.atan2(delta_y, delta_x);

	if (delta_y < 0) {
		r += (2 * Math.PI);
	}
	return r;
}

/**
 * Get degrees
 * @param	radians
 * @return
 */
function getDegrees(radians: Number): Number {
	return Math.floor(radians / (Math.PI / 180));
}
stage.addEventListener(Event.ENTER_FRAME, handleCollision);

function handleCollision(e: Event): void {
	if (mcMain.hitTestObject(_player)) {
		gotoAndStop(1)
	}
}

Response to as3 Hit test for enemy 2016-05-04 12:34:59


At 5/4/16 08:24 AM, Gimmick wrote: Please put your code in [code][/code] (replace square brackets with the angle braces <>) tags. Also, what am I supposed to be seeing again? Why is the hittest function removed? What is the hittest function, actually? Is it a simple check or something else?

sorry about that and thanks for your interest in helping me.I would like to simply remove the bullet(b) and player(_player)(not the pentagon) when they touch each other but i'm getting a couple of errors when i try to

Response to as3 Hit test for enemy 2016-05-05 06:00:40


At 5/4/16 12:34 PM, orion1220 wrote: sorry about that and thanks for your interest in helping me.I would like to simply remove the bullet(b) and player(_player)(not the pentagon) when they touch each other but i'm getting a couple of errors when i try to

What have you tried so far? What errors did you get?


Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp

"Sit look rub panda" - Alan Davies

BBS Signature