Bounce Effects With HitTestPoint
im building a flash car game where if u hit the sides of the buildings you bounce off my problem is that sometimes you get stuck in the walls and it doesnt bounce at all
heres my bounce code
Code: public function moveCar(timeDiff:Number) {
// get current position var carPos:Point = new Point(gamesprite.car.x, gamesprite.car.y);
// calculate change var carAngle:Number = gamesprite.car.rotation; var carAngleRadians:Number = (carAngle/360)*(2.0*Math.PI); var carMove:Number = speed*timeDiff; var dx:Number = Math.cos(carAngleRadians)*carMove; var dy:Number = Math.sin(carAngleRadians)*carMove;
// see if car is NOT on the road if (!gamesprite.road.hitTestPoint(carPos.x+dx+gamesprite.x, carPos.y+dy+gamesprite.y, true)) { gamesprite.car.y = gamesprite.side.y; gamesprite.car.x = gamesprite.side.x;
// see if car is on the side //bounce off if (gamesprite.side.hitTestPoint(gamesprite.car.x, gamesprite.car.y, true)) { gamesprite.y = (Math.sin(gamesprite.car.rotation * 0.018) * speed) * 5 / -1; gamesprite.x = speed * Math.cos(gamesprite.car.rotation * 0.018) * 4; speed = 0 - speed * bounce;
} }
// set new position of car gamesprite.car.x = carPos.x+dx; gamesprite.car.y = carPos.y+dy; } heres a copy of the game here
can someone help me out plz
thanks
FlashKit > Flash Help > Actionscript 3.0
Posted on: 07-07-2008, 06:56 PM
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Bounce Effect Using HitTestPoint
im building a flash car game where if u hit the sides of the buildings you bounce off
my problem is that sometimes you get stuck in the walls and it doesnt bounce at all
heres my bounce code
Code:
public function moveCar(timeDiff:Number) {
// get current position
var carPos:Point = new Point(gamesprite.car.x, gamesprite.car.y);
// calculate change
var carAngle:Number = gamesprite.car.rotation;
var carAngleRadians:Number = (carAngle/360)*(2.0*Math.PI);
var carMove:Number = speed*timeDiff;
var dx:Number = Math.cos(carAngleRadians)*carMove;
var dy:Number = Math.sin(carAngleRadians)*carMove;
// see if car is NOT on the road
if (!gamesprite.road.hitTestPoint(carPos.x+dx+gamesprite.x, carPos.y+dy+gamesprite.y, true)) {
gamesprite.car.y = gamesprite.side.y;
gamesprite.car.x = gamesprite.side.x;
// see if car is on the side
//bounce off
if (gamesprite.side.hitTestPoint(gamesprite.car.x, gamesprite.car.y, true)) {
gamesprite.y = (Math.sin(gamesprite.car.rotation * 0.018) * speed) * 5 / -1;
gamesprite.x = speed * Math.cos(gamesprite.car.rotation * 0.018) * 4;
speed = 0 - speed * bounce;
}
}
// set new position of car
gamesprite.car.x = carPos.x+dx;
gamesprite.car.y = carPos.y+dy;
}
heres a copy of the game
here
can someone help me out plz
thanks
Help With HitTestPoint And Bounce Objects
im building a flash car game where if u hit the sides of the buildings you bounce off
my problem is that sometimes you get stuck in the walls and it doesnt bounce at all
heres my bounce code
Code:
public function moveCar(timeDiff:Number) {
// get current position
var carPos:Point = localToGlobal(new Point(gamesprite.car.x, gamesprite.car.y));
// calculate change
var carAngle:Number = gamesprite.car.rotation;
var carAngleRadians:Number = (carAngle/360)*(2.0*Math.PI);
var carMove:Number = speed*timeDiff;
var dx:Number = Math.cos(carAngleRadians)*carMove;
var dy:Number = Math.sin(carAngleRadians)*carMove;
// see if car is NOT on the road
if (gamesprite.road.hitTestPoint(carPos.x+dx+gamesprite.x, carPos.y+dy+gamesprite.y, true)) {
gamesprite.y = (Math.sin(gamesprite.car.rotation * 0.018) * speed) * 6 / -1;
gamesprite.x = speed * Math.cos(gamesprite.car.rotation * 0.018) * 4;
speed = 0 - speed * bounce;
gamesprite.car.y = gamesprite.side.y;
gamesprite.car.x = gamesprite.side.x;
// see if car is on the side
/*//bounce off
if (gamesprite.side.hitTestPoint(carPos.x+dx+gamesprite.x, carPos.y+dy+gamesprite.y, true)) {
gamesprite.y = (Math.sin(gamesprite.car.rotation * 0.018) * speed) * 6 / -1;
gamesprite.x = speed * Math.cos(gamesprite.car.rotation * 0.018) * 4;
speed = 0 - speed * bounce;
}*/
}
// set new position of car
gamesprite.car.x = carPos.x+dx;
gamesprite.car.y = carPos.y+dy;
}
i can supply more code if necessary
the problem im having is that sometimes it doesnt bounce off. it will just go right into the other object and get stuck
not sure where i went wrong maybe someone can help
heres a copy of the game
http://danberko.com/adam/gameLevel1.swf
can someone help me out plz
thanks
Sin Bounce Problems. (acton Script Rollover Effects)
(USing Flash Pro 8)
I am try to get a button to move smoothly. Below is code I have been playing with. I have the button animating the way I like, but I can't figure out how to get it to stay on the x coordinate. Is there a way to counter the %150 with a certain number of pixels?
code in 1st frame
coolness = function(name, var_i, var_A){
name.i = var_i;
name.A = var_A
name.onEnterFrame=function(){
this.A *= .95;
this.i += .3;
this._yscale = this._xscale=(Math.sin(this.i)*this.A) + 150;
}
}
code on button
on(rollOver) {
coolness(this, 100 , 200);
}
Sin Bounce Problems. (acton Script Rollover Effects)
(using Flash Pro 8)
I am try to get a button to move smoothly. Below is code I have been playing with. I have the button animating the way I like, but I can't figure out how to get it to stay on the x coordinate. Is there a way to counter the %150 with a certain number of pixels?
code in 1st frame
coolness = function(name, var_i, var_A){
name.i = var_i;
name.A = var_A
name.onEnterFrame=function(){
this.A *= .95;
this.i += .3;
this._yscale = this._xscale=(Math.sin(this.i)*this.A) + 150;
}
}
code on button
on(rollOver) {
coolness(this, 100 , 200);
}
Drop, Bounce, Collide, Bounce?
Hi!
I'm trying to figure out how to get 2 (or more) movie clips to bounce off each other and it's a bit frustrating since I'm just learning HitTest etc.
What I have:
- I have 2 movie clips, a circle and a square.
- each movie clip has actionscript for the "falling" gravity to work.
- each movie clip has a button in it with actionscript for the dragging and releasing etc.
- When the movie loads, the circle and square drop from the top of the stage and bounce (using gravity etc).
- You can drag and "toss" each object and it will bounce off the sides and bottom of the stage.
What I'm trying to do:
1) I'd like the circle and square to bounce off each other when they collide just like they bounce off the sides of the stage (right now they bounce behind or in front of eachother based on their layer depths).
2) Also, but less important, I'd like them to bounce off the top of the stage too instead of flying up and out.
Questions:
- How do I make this work? I've seen many examples using HitTest etc on instances of one movieclip, but my movie clips need to be different since they will be turned into separate objects/graphics later (if that makes sense). Is this still a HitTest solution? I don't think so since I'm not really using HitTest to begin with? I've tried incorporating code from some of those examples, but it just doesn't work.
I've attached 2 files - one in Flash 8 format, and another in Flash mx2004 format so hopefully someone can open one of them, take a look at the code and give me some help.
I appreciate it
Peace,
Hondo311
HitTestPoint
I do not understand hitTestPoint, what does it exactly do?
Why does this code not work?
It won't hit...
ActionScript Code:
if (h.hitTestPoint(Ehelis[EheliNum].x, Ehelis[EheliNum].y, true)) {
trace("hit")
}
HitTestPoint?
Can you do a hitTestPoint on only half of an mc? eg, only to test if the players legs are touching the ground.
HitTestPoint Bug
I made this test file so you can see what I mean about this hitTestPoint() bug.
The area on the stage that is registering the "hit" is never in the same place as the visible MC that is used as the hit tester. IE the spot that results as true when rolled over is always some where off and to the right of the actual MC. Just move your mouse around the stage and you'll see what I mean. Things are not lining up. There is absolutely nothing else in the hit tested MC at all. No invisible assets within it etc... Just what you see.
I had it in a few classes before and I have tested it quite a bit and I always get the same thing.
Any thoughts?
the timeline based code:
ActionScript Code:
import flash.utils.*;
var _hitTestTimer:Timer = new Timer(50);
_hitTestTimer.addEventListener (TimerEvent.TIMER, hitTester);
_hitTestTimer.start ();
function hitTester (evt:Event) {
//if (testMC.hitTestPoint(mouseY, mouseX, true)) {
if (testMC.hitTestPoint(mouseY, mouseX, false)) {
testMC.alpha = .5;
} else {
testMC.alpha = 1;
}
}
HitTestPoint() Bug
I made this test file so you can see what I mean about this hitTestPoint() bug.
Heres the file swf online
The area on the stage that is registering the "hit" is never in the same place as the visible MC that is used as the hit tester. IE the spot that results as true when rolled over is always some where off and to the right of the actual MC. Just move your mouse around the stage and you'll see what I mean. Things are not lining up. There is absolutely nothing else in the hit tested MC at all. No invisible assets within it etc... Just what you see.
I had it in a few classes before and I have tested it quite a bit and I always get the same thing.
Any thoughts?
Heres the timeline code:
Attach Code
import flash.utils.*;
var _hitTestTimer:Timer = new Timer(50);
_hitTestTimer.addEventListener (TimerEvent.TIMER, hitTester);
_hitTestTimer.start ();
function hitTester (evt:Event) {
//if (testMC.hitTestPoint(mouseY, mouseX, true)) {
if (testMC.hitTestPoint(mouseY, mouseX, false)) {
testMC.alpha = .5;
} else {
testMC.alpha = 1;
}
}
HitTestPoint
I have this code so that when a spaceship goes through a blackhole it plays the spaceships moveiclip. This works fine on the first part of the code when the user just clicks the start button. However, i have put in a difficulty setting and when you change the difficulty setting the hittest doesn't work anymoreand there are no error messages. I have tried putting the blackHoles array in the difficulty function but nothing changes and i didn't really think this would make a difference as the easy function is already using the blackHoles array outside the difficulty function. Can anyone tell me whats wrong?
Attach Code
var blackHoles:Array = new Array();
function position(target) {
target.x = Math.random() * stage.stageWidth;
target.y = Math.random() * stage.stageHeight;
for (var i:uint=0; i<blackHoles.length; i++) {
if(target.hitTestObject(blackHoles[i])){
position(target);
return false;
}
}
}
startButton.addEventListener(MouseEvent.CLICK, startGame);
function startGame(evt:MouseEvent):void{
var moon = new Moon;
moon.x = 250;
moon.y = 320;
addChild(moon);
for(var i:int = 0; i < 10; i++)
{
var blackHole:BlackHole = new BlackHole();
blackHole +i;
blackHole.x = Math.random() * stage.stageWidth;
blackHole.y = Math.random() * stage.stageHeight;
position(blackHole);
blackHoles.push(blackHole);
addChild(blackHole);
}
stage.addEventListener(Event.ENTER_FRAME, hitTest);
function hitTest(evt:Event):void
{
for(var i:int = 0; i < blackHoles.length; i++)
{
if (spaceship.hitTestPoint(blackHoles[i].x, blackHoles[i].y, true))
{
spaceship.gotoAndPlay(1);
}
}
}
difficulty.addEventListener(MouseEvent.CLICK, chooseDifficulty);
function chooseDifficulty(evt:MouseEvent):void{
var easy:Easy = new Easy();
var normal:Normal = new Normal();
var hard:Hard = new Hard();
easy.x = 210;
easy.y = 260;
normal.x = 210;
normal.y = 300;
hard.x = 210;
hard.y = 340;
addChild(easy);
addChild(normal);
addChild(hard);
removeChild(startButton);
removeChild(difficulty);
removeChild(exit);
easy.addEventListener(MouseEvent.CLICK, chooseEasy);
function chooseEasy(evt:MouseEvent):void{
var moon = new Moon;
moon.x = 250;
moon.y = 320;
addChild(moon);
for(var i:int = 0; i < 5; i++)
{
var blackHole:BlackHole = new BlackHole();
blackHole +i;
blackHole.x = Math.random() * stage.stageWidth;
blackHole.y = Math.random() * stage.stageHeight;
position(blackHole);
blackHoles.push(blackHole);
addChild(blackHole);
}
stage.addEventListener(Event.ENTER_FRAME, hitTest);
function hitTest(evt:Event):void
{
for(var i:int = 0; i < blackHoles.length; i++)
{
if (spaceship.hitTestPoint(blackHoles[i].x, blackHoles[i].y, true));
{
spaceship.gotoAndPlay(1);
}
}
}
timer.start();
addChild(spaceship);
spaceship.x = 80;
spaceship.y = 80;
removeChild(easy);
removeChild(normal);
removeChild(hard);
removeChild(splashScreen);
}
Edited: 12/30/2008 at 08:42:43 AM by g-rebel
HitTestPoint Help
I can't figure out hitTestPoint. I'm trying to make this map of the US and each state needs to do something when you mouse over the state, but the overlaps are killing me as movie clips are square and states are not. I'm new to AS3 and was hoping someone could help me out.
Help With HitTestPoint
Hi,
hitTestPoint works fine in my application until the application is scaled. I'm hoping that someone can explain where I've gone wrong. In the following details the 'source' is a sprite being dragged over the 'target'.
Display Hierarchy:
stage
frame [ frame.swf ]
content [ content.swf ]
source & target sprites
Hit Test Pseudo-code [ content.swf ]:
var pt:Point = source.parent.localToGlobal( new Point( source.x, source.y ) );
if( target.hitTestPoint( pt.x, pt.y, true ) )
{ ... }
Scaling Pseudo-code [ frame.swf ]:
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
...
scaleX = <computed scale>;
scaleY = <computed scale>;
Cheers!
HitTestPoint Problem
I am trying to create a space game where you have to fly around black holes. I have made a difficulty setting where easy has less black holes and hard has more. In creating the difficulty setting my hitTestPoint code has stopped working and i'm not sure why. Here is the code i am using -
Code:
difficulty.addEventListener(MouseEvent.CLICK, chooseDifficulty);
function chooseDifficulty(evt:MouseEvent):void{
var easy:Easy = new Easy();
var normal:Normal = new Normal();
var hard:Hard = new Hard();
easy.x = 210;
easy.y = 260;
normal.x = 210;
normal.y = 300;
hard.x = 210;
hard.y = 340;
addChild(easy);
addChild(normal);
addChild(hard);
removeChild(startButton);
removeChild(difficulty);
removeChild(exit);
easy.addEventListener(MouseEvent.CLICK, chooseEasy);
function chooseEasy(evt:MouseEvent):void{
for(var i:int = 0; i < 5; i++)
{
var blackHole:BlackHole = new BlackHole();
blackHole.name = "BlackHole" +i;
blackHole.x = Math.random() * stage.stageWidth;
blackHole.y = Math.random() * stage.stageHeight;
addChild(blackHole);
}
splashScreen.visible = false;
timer.start();
addChild(spaceship);
spaceship.x = 80;
spaceship.y = 80;
removeChild(easy);
removeChild(normal);
removeChild(hard);
}
}
stage.addEventListener(Event.ENTER_FRAME, hitTest)
function hitTest(evt:Event):void{
if (spaceship.hitTestPoint(blackHole.x, blackHole.y, true))
{
removeChild(spaceship);
}
}
Any ideas?
HitTestPoint Issues
Hi, I'm new to Flash and Actionscript, but I'm an experienced javascript developer, so I thought I'd jump straight in to AS3 by creating a side-scrolling game. I've hit a snag and was wondering if anyone could point me in the right direction. I've got a MovieClip named "man", who is an instance of my MoveCharacter class that I was hoping to use as a base class for movement, physics, and collision detection in any side-scrolling game. I'm using hitTestPoint to try to test a collision with walls of a level (Ob) that is made up of simple line-tool vector graphics. If the shapeFlag argument is set to true, no collisions with Ob are detected, and if shapeFlag is false, collisions are detected anywhere within the Ob MovieClip, so that the character simply won't move. My question is: how do I accurately test collisions against Ob?
MoveCharacter
Code:
package
{
import flash.events.*;
import flash.display.*;
import flash.ui.Keyboard;
import flash.geom.*;
import KeyPoll;
import Main;
import World;
import Ob;
public class MoveCharacter extends MovieClip
{
public var xspeed:Number = 0;
public var yspeed:Number = 0;
public var hitwalll:Boolean = false;
public var hitwallr:Boolean = false;
public var gravity:Number = 0;
public var jumping:Boolean = false;
public var running:Boolean = false;
public static var world:World;
public static var objects:Ob;
private var key:KeyPoll;
public function MoveCharacter()
{
this.addEventListener(Event.ADDED_TO_STAGE, onStageReady)
function onStageReady(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onStageReady);
trace("MoveCharacter defined and initialized");
key = new KeyPoll(stage);
objects = new Ob();
world = new World();
stage.addChild(world);
world.x = 0; world.y = 0;
stage.addChild(objects);
objects.x = 100.6; objects.y = -700.4;
stage.addEventListener(Event.ENTER_FRAME, moveAround);
function moveAround(event:Event):void
{
var pointtop:Point = new Point(x + (width / 2), y);
var pointbottom:Point = new Point (x + (width / 2), y + height);
var pointleft:Point = new Point (x, y + (height / 2));
var pointright:Point = new Point (x + width, y + (height / 2));
if (objects.hitTestPoint(pointright.x, pointright.y, false) == true)
{
hitwallr = true;
}
if (objects.hitTestPoint(pointleft.x, pointleft.y, false) == true)
{
hitwalll = true;
}
if(key.isDown(Keyboard.RIGHT))
{
trace("Right");
if (!hitwallr)
{
running = true;
x+=5;
world.x+=5;
trace(pointright);
}
trace(hitwallr);
scaleX = 1;
hitwallr = false;
}
if(key.isDown(Keyboard.LEFT)){
trace("Left");
if (!hitwalll)
{
running = true;
x-=5;
trace(pointleft);
}
trace(hitwalll);
scaleX = -1;
hitwalll = false;
}
if(key.isDown(Keyboard.UP))
{
trace("Up");
}
if(key.isDown(Keyboard.DOWN))
{
trace("Down");
}
}
}
}
}
}
HitTestPoint Problem
I am trying to create a space game where you have to fly around black holes. I have made a difficulty setting where easy has less black holes and hard has more. In creating the difficulty setting my hitTestPoint code has stopped working and i'm not sure why. I have attached the code i am using, can anyone help?
Attach Code
difficulty.addEventListener(MouseEvent.CLICK, chooseDifficulty);
function chooseDifficulty(evt:MouseEvent):void{
var easy:Easy = new Easy();
var normal:Normal = new Normal();
var hard:Hard = new Hard();
easy.x = 210;
easy.y = 260;
normal.x = 210;
normal.y = 300;
hard.x = 210;
hard.y = 340;
addChild(easy);
addChild(normal);
addChild(hard);
removeChild(startButton);
removeChild(difficulty);
removeChild(exit);
easy.addEventListener(MouseEvent.CLICK, chooseEasy);
function chooseEasy(evt:MouseEvent):void{
for(var i:int = 0; i < 5; i++)
{
var blackHole:BlackHole = new BlackHole();
blackHole.name = "BlackHole" +i;
blackHole.x = Math.random() * stage.stageWidth;
blackHole.y = Math.random() * stage.stageHeight;
addChild(blackHole);
}
splashScreen.visible = false;
timer.start();
addChild(spaceship);
spaceship.x = 80;
spaceship.y = 80;
removeChild(easy);
removeChild(normal);
removeChild(hard);
}
}
stage.addEventListener(Event.ENTER_FRAME, hitTest)
function hitTest(evt:Event):void{
if (spaceship.hitTestPoint(blackHole.x, blackHole.y, true))
{
removeChild(spaceship);
}
}
HitTestPoint Problem
I have some code that takes a movieclip called blackHole and places 10 instances of them randomly on the stage. I then want another movieclip called spaceship to collide with these instances. The problem is the spaceship movieclip only collides with one instance of the blackHole movieclip. Any ideas?
Attach Code
startButton.addEventListener(MouseEvent.CLICK, startGame);
function startGame(evt:MouseEvent):void{
var moon = new Moon;
moon.x = 250;
moon.y = 320;
addChild(moon);
for(var i:int = 0; i < 10; i++)
{
var blackHole:BlackHole = new BlackHole();
blackHole +i;
blackHole.x = Math.random() * stage.stageWidth;
blackHole.y = Math.random() * stage.stageHeight;
position(blackHole);
blackHoles.push(blackHole);
addChild(blackHole);
}
stage.addEventListener(Event.ENTER_FRAME, hitTest);
function hitTest(evt:Event):void
{
if (spaceship.hitTestPoint(blackHole.x, blackHole.y, true))
{
spaceship.gotoAndPlay(1);
}
}
timer.start();
addChild(spaceship);
spaceship.x = 80;
spaceship.y = 80;
removeChild(startButton);
removeChild(difficulty);
removeChild(exit);
removeChild(splashScreen);
}
HitTestPoint On Transparent PNG
Hey all,
So i have a transparent .png that is about 100px by 200px with a picture of a man in the middle.
I want to click on the man and only the man to have my function fire but it seems like if i click within the bounding box, the function still fires.
I'm using the hitTestPoint function that seems to say I can specify whether I want to do an alpha check or a shape check. Both of those options seem to do the same thing when I run it real time.
Any ideas?
Thanks!
HitTestPoint Problem
Hi. I have created a lunar lander game where you have to avoid blackholes and land on the moon. I have used hitTestPoint for the collision between the spaceship and the blackholes, this hitTest works fine but I have tried to add an difficulty setting where easy has less blackholes and hard has more. However, hitTestPoint doesn't work when you change the difficulty setting and i'm not sure why. It doesn't give an error, it just doesn't work. Here's my code -
Code:
var blackHoles:Array = new Array();
function position(target) {
target.x = Math.random() * stage.stageWidth;
target.y = Math.random() * stage.stageHeight;
for (var i:uint=0; i<blackHoles.length; i++) {
if(target.hitTestObject(blackHoles[i])){
position(target);
return false;
}
}
}
startButton.addEventListener(MouseEvent.CLICK, startGame);
function startGame(evt:MouseEvent):void{
moon.visible = true;;
for(var i:int = 0; i < 10; i++)
{
var blackHole:BlackHole = new BlackHole();
blackHole +i;
blackHole.x = Math.random() * stage.stageWidth;
blackHole.y = Math.random() * stage.stageHeight;
position(blackHole);
blackHoles.push(blackHole);
addChild(blackHole);
}
stage.addEventListener(Event.ENTER_FRAME, hitTest);
function hitTest(evt:Event):void
{
for(var i:int = 0; i < 10; i++)
{
if (spaceship.hitTestPoint(blackHoles[i].x, blackHoles[i].y, true))
{
spaceship.gotoAndPlay(1);
}
}
}
function chooseDifficulty(evt:MouseEvent):void{
var easy:Easy = new Easy();
var normal:Normal = new Normal();
var hard:Hard = new Hard();
easy.x = 210;
easy.y = 260;
normal.x = 210;
normal.y = 300;
hard.x = 210;
hard.y = 340;
addChild(easy);
addChild(normal);
addChild(hard);
removeChild(startButton);
removeChild(instructions);
removeChild(difficulty);
removeChild(exit);
easy.addEventListener(MouseEvent.CLICK, chooseEasy);
function chooseEasy(evt:MouseEvent):void{
var moon = new Moon;
moon.x = 250;
moon.y = 320;
addChild(moon);
for(var i:int = 0; i < 5; i++)
{
var blackHole:BlackHole = new BlackHole();
blackHole +i;
blackHole.x = Math.random() * stage.stageWidth;
blackHole.y = Math.random() * stage.stageHeight;
position(blackHole);
blackHoles.push(blackHole);
//blackHoles.push(moon);
//blackHoles.push(gameTimer_tb);
//blackHoles.push(lives_tb);
//blackHoles.push(spaceship_mc);
addChild(blackHole);
}
stage.addEventListener(Event.ENTER_FRAME, hitTest);
function hitTest(evt:Event):void
{
for(var i:int = 0; i < 5; i++)
{
if (spaceship.hitTestPoint(blackHoles[i].x, blackHoles[i].y, true));
{
spaceship.gotoAndPlay(1);
}
}
}
I then have a normal setting which is 10 blackHoles and a hard setting which is 15 blackholes, coded in the same way. Can anyone help?
Movie Bounce In Bounce Out
Hi
I want to make a menu that bounces in on key press (for ipaq ppc)and when pressed again with the stylus bounces back out. I am using Flash MX but can not get this to work. Any ideas gretefully received.
Thanks
Bounce, Collide, Bounce Again?
Hi!
Flash 8 Actionscript question:
I'm trying to figure out how to get 2 (or more) movie clips to bounce off each other and it's a bit frustrating since I'm just learning HitTest etc.
What I have:
- I have 2 movie clips, a circle and a square.
- each movie clip has actionscript for the "falling" gravity to work.
- each movie clip has a button in it with actionscript for the dragging and releasing etc.
- When the movie loads, the circle and square drop from the top of the stage and bounce (using gravity etc).
- You can drag and "throw each" object and it will bounce off the sides and bottom of the stage.
What I'm trying to do:
1) I'd like the circle and square to bounce off each other when they collide just like they bounce off the sides of the stage (right now they bounce behind or in front of eachother based on their layer depths).
2) Also, but less important, I'd like them to bounce off the top of the stage too instead of flying up and out.
Questions:
- How do I make this work? I've seen many examples using HitTest etc on instances of one movieclip, but my movie clips need to be different since they will be turned into separate objects/graphics later (if that makes sense). Is this still a HitTest solution? I don't think so since I'm not really using HitTest to begin with? I've tried incorporating code from some of those examples, but it just doesn't work.
I've attached 2 files - one in Flash 8 format, and another in Flash mx2004 format so hopefully someone can open one of them, take a look at the code and give me some help.
I appreciate it
Peace,
Hondo311
HitTestPoint Vs Array Read
I was wondering if some one could answer the folllowing question i have. How much slower on the cpu would doing a hitTestPoint (1 point to one very large vector shape lets say 2500*2500) than reading a position value in a map array.
Is using hitTestPoint on a complex shape much slower than a simple shape please enlighten me on this critical subject thanks in advance.
HitTestPoint Against Global Variables?
I'm storing all variables that I want to be global--accessible throughout the project--in a separate class in a static Object variable. This seems to work fine except for one problem.
I have a Sprite that is added to the stage on frame 1 of my flash file and then passed to my GlobalVarContainer. A ball with a separate class definition is later added and interacts with the Sprite.
The problem is that the ball's class actionscript is using hitTestPoint with the shapeFlag Boolean set to true to detect a collision with aforementioned Sprite that is stored in the static Object variable.
But the shapeFlag is not working and the ball is colliding with the bounding box of the sprite.
Here is the code from my Ball class that references the object:
Code:
if (GlobalVarContainer.vars.terrainium.hitTestPoint(spot_x, spot_y, true)){
Can anyone tell me why it is not detecting a pixel-level collision but uses a bounding box despite the shapeFlag Boolean being set to true?
I will elaborate more on my code if necessary. Thanks for your help!
Trouble With HitTestPoint And Tween
Hi guys,
I'm having a trouble with hitTestPoint and tween. Well, if you look at the code you will understand what I am trying to archive here.
---------------------
var action:Tween;
target01.alpha = 0.6;
target01.addEventListener(Event.ENTER_FRAME, transition01);
function transition01(event:Event):void {
if (target01.hitTestPoint(mouseX, mouseY, true)) {
action = new Tween(target01, "alpha", Strong.easeOut,0.6, 1, 1, true);
} else {
action = new Tween(target01, "alpha", Strong.easeOut,1, 0.6, 1, true);
}
}
---------------------
Yes, that's right. I'm trying to make the movie clip 'target01' to fade in once the mouse pointer roll-overs, and fades out when roll-out. But tween actions don't work...
Yes, I did MouseEvents instead, but it is not the way I want it.. Please help me outta here guys.
HitTestPoint On Objects Which Are Not On The Displaylist
I want to do a hitTestPoint on an object which isn't on the displaylist. When setting the shapeFlag to false (default), this will work without any problems. As soon as this is set to true, it alway's returns false.
Is this the behaviour we should expect, or is this some kind of flash bug? Does anyone know a workaround?
As soon as I add the displayObject to the displaylist this will work fine. But it's simply not an option to have it rendered visually, cause of performance reasons.
HitTestPoint And Custom Method
Hi,
I have some little glitches using hitTestPoint, On canvas Shapes laying on an other large canvas.
ActionScript Code:
// on mouse down
// 'This' is the board canvas
trace('point ', this.contentMouseX, this.contentMouseY);
trace(currentShape.hitTestPoint(this.contentMouseX, this.contentMouseY, true))
trace('curshape', currentShape.x, currentShape.y, currentShape.x+currentShape.width, currentShape.y+currentShape.height);
cruShape is the previously drawn shape.
> point 342, 252
> false
> curShape, 320,220,700,360
We agree that 342, 252 is inside the shape but why does the hitpoint test fails? Somethind to do with the coordinates of curShape it uses? how can i tell him to use contentMouseX,Y ?
Thanks
>
MovieClip Control / HitTestPoint...I Think
OK, so, I want to make a movieclip play forward and backward based on whether or not the hit Test Point returns true or false, right? Keep in mind, I'm extremely stupid, so I am not going to intuitively construct my AS 3 code correctly.
I used to use this:
on ClipEvent (enterFrame){
if (this.hitTest (root.xmouse, root.ymouse, true)) {
this.nextFrame ();
}else{
this.prevFrame ();
}
}
But, of course, that was too easy. Now, with AS 3, they've deprecated all my code so I have to use event handlers, import classes, blah blah blah. The compiler is treating me like crap, complaining about everything I do...reminds me of my ex wife.
Anyway, it's not working. Is there anyone who could tell me the simple code to use to test if the mouse is over my MovieClip and play clip forward and backward? I can't imagine it's so difficult, but I've been at this for 3 days to no avail. Remember, I'm not a programmer, just a stupid, unemployed carpenter, so please don't assume I can
just put the rest of the code in...I'm sure I can't. I've been trying to do that for many many hours.
Thanks!
Weird Behavior Of HitTestPoint And RemoveChild()
Hi
I am trying to drag and drop multiple instances of UILoader on stage. My goal is if user puts UILaoder partially on top of other UILoader , it should just stay on top of it. If user puts on top of UILoader more than the central point of other UILoader, the other UILoader will change the color of its border and on mouse release gets replaced by the placed UILoader.
I am successful in getting all this but I am getting inconsistent results as an output. Sometimes I have to put exactly on top of other UILoader to replace it and sometimes I just touch the UILoader and it shows red border and gets replaced. I used the logic of calculating the center of rectangle(my UILoader container) and use hitPoint method, and as soon as dragged UILoader hits the calculated center of UILoader, it gets replaced.
Please help me out in where I am messing things up.Any better idea than this will also be highly appreciated.
( Following is the function which is telling my UILoader to replace of stopDrag.)
Thanks a lot
Anuj
/*************************CODE***************/
function dropUILoader(e:MouseEvent):void
{
if(e.target is UILoader)
{
var myUILoader:UILoader = e.target as UILoader;
if (mouseY<746)
{
myUILoader.stopDrag();
//Putting video on top of already existed videos
var trackChild:Number=container.getChildIndex(myUILoad er);
var childContainerisplayObject= container.getChildAt(trackChild);
for (var z:Number=0; z<=container.numChildren-1; z++)
{
var restChildisplayObject=container.getChildAt(z);
if (childContainer!=restChild)
{
if(restChild is UILoader)
{
var cRC:UILoader=restChild as UILoader;
var regcRCx:Number=cRC.x;
var regcRCy:Number=cRC.y;
var regcRCWidth:Number=cRC.width;
var regcRCHeight:Number=cRC.height;
if(childContainer.hitTestPoint(((regcRCx+regcRCWid th)/2),(regcRCy+regcRCHeight)/2)==true)
{
trace("Totally Hit");
container.removeChild(cRC);
}
else
{
trace("Partially Hit");
}
}
}
}
}
else
{
myUILoader.unload();
container.removeChild(myUILoader);
}
}
}
HitTestPoint With Nested Movie Clips?
Hi. I'm trying to make a top-down racing game, and I'm having some trouble with hitTestPoint.
I'm trying to detect a collision between each of the corners of the car (a movie clip) and the track wall (a long curvy shape inside a movie clip).
I have hitTestPoint working fine between the wall and the registration of the car movie clip (which is in the center), but I can't get it to work between the wall and a corner of the car.
I tried two things: First, I tried putting a movie clip at each corner inside the car movie clip and doing a hit test between the corner movie clip and the wall, but when I did that, it didn't detect a collision or do anything.
I also tried adding to the car.x and car.y in hitTestPoint to offset the spot it was checking, but it did that relative to the stage, not relative to the car movie clip like I need (because the car's rotation changes).
Does anyone have any idea how to do this? Is it just impossible to do hitTestPoint within nested movie clips? Am I just doing it all wrong? Any help is appreciated.
Weird Behavior Of HitTestPoint And RemoveChild()
Hi
I am trying to drag and drop multiple instances of UILoader on stage. My goal is if user puts UILaoder partially on top of other UILoader , it should just stay on top of it. If user puts on top of UILoader more than the central point of other UILoader, the other UILoader will change the color of its border and on mouse release gets replaced by the placed UILoader.
I am successful in getting all this but I am getting inconsistent results as an output. Sometimes I have to put exactly on top of other UILoader to replace it and sometimes I just touch the UILoader and it shows red border and gets replaced. I used the logic of calculating the center of rectangle(my UILoader container) and use hitPoint method, and as soon as dragged UILoader hits the calculated center of UILoader, it gets replaced.
Please help me out in where I am messing things up.Any better idea than this will also be highly appreciated.
( Following is the function which is telling my UILoader to replace of stopDrag.)
Thanks a lot
Anuj
/*************************CODE***************/
function dropUILoader(e:MouseEvent):void
{
if(e.target is UILoader)
{
var myUILoader:UILoader = e.target as UILoader;
if (mouseY<746)
{
myUILoader.stopDrag();
//Putting video on top of already existed videos
var trackChild:Number=container.getChildIndex(myUILoad er);
var childContainerisplayObject= container.getChildAt(trackChild);
for (var z:Number=0; z<=container.numChildren-1; z++)
{
var restChildisplayObject=container.getChildAt(z);
if (childContainer!=restChild)
{
if(restChild is UILoader)
{
var cRC:UILoader=restChild as UILoader;
var regcRCx:Number=cRC.x;
var regcRCy:Number=cRC.y;
var regcRCWidth:Number=cRC.width;
var regcRCHeight:Number=cRC.height;
if(childContainer.hitTestPoint(((regcRCx+regcRCWid th)/2),(regcRCy+regcRCHeight)/2)==true)
{
trace("Totally Hit");
container.removeChild(cRC);
}
else
{
trace("Partially Hit");
}
}
}
}
}
else
{
myUILoader.unload();
container.removeChild(myUILoader);
}
}
}
Moving A Dynamically Added MC On A HitTestPoint
Hi,
I was reading through one of the tutorials from kirupa found here http://www.kirupa.com/developer/flas...es_AS3_pg2.htm
I got a lot out of it =]
but when i tried modifying the code to add a hitTest on it, it became "weird"
here is my .as file
package {
import flash.display.*;
import flash.events.*;
import flash.geom.Point;
public class Sq extends MovieClip {
var radians =0;
var speed =0;
var radius =0;
var randomValue=0;
public function Sq() {
speed =.01+.5*Math.random();
radius =2+10*Math.random();
randomValue= Math.random()*1;
this.x = Math.random()*550;
this.y = Math.random()*400;
this.rotation = Math.random()*360;
this.scaleX = this.scaleY = randomValue;
this.alpha = 1-randomValue;
this.addEventListener(Event.ENTER_FRAME, rotateSq);
}
function rotateSq(e:Event) {
if (this.hitTestPoint(mouseX,mouseY,true)) {
radians+=speed;
//trace("test works");
this.x+=Math.round(radius*Math.cos(radians));
this.y+=Math.round(radius*Math.sin(radians));
}else{
trace("nope");
}
}
}
}
the effect i'm trying to accomplish is when the user rolls over one of the MC's added
by the following function inside the .fla
function AddSq() {
for (var i:int =0; i<50; i++) {
var newSq:Sq = new Sq();
this.addChild(newSq);
}
}
AddSq();
is that the MC(which is a square) starts moving in a circle. but it seems that the hit area is in a different location and the square is in another.
is there something i'm doing wrong?
Thanks!
Other Ways Of Collision Detection Other Than HitTestPoint/Object
Hello all,
I'm way stuck here. The thing I'm trying to do is to detect collision with one object to another. I've tried using hitTestObject and also tried to use intersect by creating rectangles around the objects and testing for intersecting. Either works great as long as there is only object near the sprite I am controlling.
BUT, I have 82 objects on the screen and some of the objects are clustered (but not overlapping). When I move my sprite to the clusters, it detects the collision from an object that is defined sooner within the loop as I check each item in my array. (not sure how else to put that.) Essentially the detection is found is determined by which object is tested first for collision. For example:
If we had objects X, Y, Z and S was the sprite. Given this format:
X Y Z
and the letter 'S' is right under the Y.
Upon testing to see which letter was closest, you'd think that Y would be detected over any of the other letters. (Assume the letters are on rectangular shaped tile too). Each letter is stored into an array and upon collision detection, it's looped thru to test collision detection. Because X is in the array sooner (position wise), it tests as true and the result i get from flash is, the closest letter is X, not Y.
hitTestObject seems to be unreliable in the case of many many objects all around. Is there any way I can detect a collision?
Any hints are appreciated,
-m
Major Slowdown From Multiple Enemies In Game.. (hitTestPoint?)
I'm trying to create a game involving enemies that spawn from the ceiling and fall onto platforms. From their they walk to the end of their platform and fall, and then turn around, repeating the process. This involves a few for loops (some double), and hitTestPoint tests.
There is about 70 platforms on screen to which each enemy is being collision checked against at all times. I realize this is a lot, but I didn't think it would be near enough to slow the game down completely.
As a test, my code currently spawns 30 enemies onscreen, which is a lot but isn't too unrealistic in comparison to what I need. Once at least 3 enemies get on screen, things start to slow down a lot. Especially when an enemy is in freefall (which is weird considering I believe that is when less calculations occur).
I have posted example code, and attached the project itself for more detail.
I keep getting too discouraged when I encounter problems like this and stop my projects.. so I hope this is easy to resolve.
Code:
public function tick(event:TimerEvent){
var i:int;
for(i=0;i<enemyList.length;i++){
//trace(enemyList[i].vy+" "+enemyList[i].vx);
enemyList[i].vy+=gravity;
if(enemyList[i].onGround){
if( Math.floor((enemyList[i].y-(enemyList[i].height/2)) / gridSize) % 2 == 0) enemyList[i].direction=1;
else enemyList[i].direction=-1;
enemyList[i].walk();
}
else enemyList[i].falling();
var collision=enemyCollisionFloor(enemyList[i]);
if(collision != -1){
//trace(collision);
enemyList[i].y = collision;
enemyList[i].vy=0;
}
if(enemyList[i].vy==0) enemyList[i].onGround=true;
else enemyList[i].onGround=false;
enemyList[i].applyFriction(0.8);
enemyList[i].updatePos();
}
}
...
public function enemyCollisionFloor(enemy:Enemy):int{
var i:int;
var p:int;
var r:int;
for(i=0;i<groundList.length;i++){
for(p=1;p<=enemy.vy;p++)
//for(r=0;r<enemy.width;r++)
if(groundList[i].hitBox.hitTestPoint(enemy.x+enemy.vx,enemy.y+p,false) ||
groundList[i].hitBox.hitTestPoint(enemy.x+(enemy.width/2)+enemy.vx,enemy.y+p,false) ||
groundList[i].hitBox.hitTestPoint(enemy.x+(enemy.width)+enemy.vx,enemy.y+p,false)){
//trace("hitbox y: "+(groundList[i].y+groundList[i].hitBox.y)+" vy: "+vy+" enemy y: "+enemy.y+" collision y: "+(enemy.y+p));
return groundList[i].y+groundList[i].hitBox.y;
}
}
return -1;
}
Bouncing Effects And Elastics Effects
i read some tutorials about some clips that includes bouncing or elastic effects, the problem is i can't still figure out some axnscripts such as MovieClip._x*=100 or MovieClip._x-=100, what does these syntax mean anyway?
can i have some tutorial like a dribbling ball that bounces a lot then settles down after a few moment?
Bounce Down...?
I want to have an object drop down to like 200px from the top, not hit anything, but then realistically bounce up and down slower and slower, like you know... it doesn't just come down and thats it. Kind of hard to explain, but I hope you get it and can help! Thanks!
Bounce, C'mon Bounce
Hiya,
Pretty new to all this, but can somebody point me in the direction of anywhere that'll explain how to get objects of different dimensions to float around in a square/circle bouncing of each other and the walls. The bouncing bits need to be buttons, incidentally.
Thank you!
Bounce
Flash MX
ok, I want to do the simple task of, when an object moves, whenever it hits any other object, it bounces at the angle opposite frome whence it came. I also want to make the object move to begin with.
so, if there is a circle in the frame, and it hits the side at a 30 degree angle, it goes off at a thirty degree angle. Physics.
Bounce
cant get ball to bounce off of a wall on both side
its a short little wall in the center of my screen and i want the ball to bounce off of it on the left and the right side.
Bounce
how can you make an object bounce against a wall?
Get A Little Bounce (MX)
Hi all,
I'm rather new to all this and have what will hopefully be a straight forward question.
I want to animate an object, with actionscript, falling from the top of the screen to land with a bounce. No interactivty nothing.
Can any one point me in the right direction?
Thanks
Bounce
how do i make a symbol bounce around the document at a slow speed useing code, so i dont have to animate the whole thing
Bounce
I really need help but I can't explain myself very well, so I will just send you to the swf file and maybe you can help me. here is the link: http://www.newgrounds.com/portal/view/174139 The problem is when you run into the planet, I want the ship to bounce off rather than stick in it like it is mud. I am simply implementing the use of two movement vars. (xv and yv) so currently, I just set them to zero. I have tried stuff like xv = -xv and I even tried the long way 'round if xv > 0 .....
If you have any suggestions please post them
thanks!
BOunce Bounce
Hi,
i am using MX 2000 & am looking for a script to create something like the bounce effect on the menu in this URL: www.hoitytoity.co.za
thx,
A.
Bounce
Hello again, I had been making a simple game to practice my actionscripting,
It's a ball that moves with the direction keys,
What I did was make 4 separate Mc's as walls; top bottom left and right,
I was able to make the ball bounce from these Mc's by using hitTest and x / yspeed - x / yspeed*2,
that worked by making the ball "bounce" in the opposite direction at the same speed as when it hit,
I've now changed the walls into one wall, I've changed the code to x / y *= -1,
That inverts the direction BUT...
When it hits it goes literally opposite direction so x and y is inverted not creating the "bounce" effect.
Is there another way that this could be achieved?
Thank you in advance!
Attach Code
onClipEvent(load) {
speed = 3;
friction = 0.96;
yspeed = 0;
xspeed = 0;
}
onClipEvent(enterFrame) {
//calculationssss
_y += yspeed;
_x += xspeed;
yspeed *= friction;
xspeed *= friction;
_rotation += xspeed;
//movement
if(Key.isDown(Key.DOWN)){
yspeed += speed;
}
if(Key.isDown(Key.UP)){
yspeed -= speed;
}
if(Key.isDown(Key.RIGHT)){
xspeed += speed;
}
if(Key.isDown(Key.LEFT)){
xspeed -= speed;
}
if(_root.wall.hitTest(_x,_y, true)) {
yspeed *= -1;
xspeed *= -1;
}
}
Help With Slide And Bounce
I'm trying to make some text slide onto the screen and then bounce naturally when it hits its required position.
I would also like to fade it it as it slide.
I'm sure all this can be done with actionscript but all I get so far is the slide movement.
Drag And Bounce
i saw a cool effect at the misssixty.com site. there is an image that you can drag upwards, then you let go and the image falls down and bounces up again. the jumps get smaller and smaller like they would in the real world. any ideas on how to do this. or any tutorials would be great.
|