Copy link to clipboard
Copied
Hey guys,
So I'm making a program that generates circles where you click, and I want to apply some physics to them. I was thinking about creating a new class that contains all the physics code and then applying it to the circles that are created, is that the way to do it?
I can't use the variable name of the circle in the physics class. And some stuff on the web said to make the variable public and static,but I create the variable INSIDE a function, and it won't let me make it public or static there. Even if I do put it in the class, I get the same error (access of undefined property circle). Here's the code :
----------------------------------------------------------------------------------------------------------------------------------
package {
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Main extends MovieClip
{
var pop:PopSound;
var soundChannel:SoundChannel;
var circleReady:Boolean = new Boolean;
var coolDown:Timer;
public function Main()
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick);
pop = new PopSound();
soundChannel = new SoundChannel();
circleReady = true;
coolDown = new Timer (500, 1);
}
function mouseClick(e:MouseEvent):void
{
if (circleReady == true)
{
var theX:Number = mouseX;
var theY:Number = mouseY;
var circle:Shape = new Shape();
circle.graphics.beginFill((Math.random() * 0xFFFFFF), 1.0);
circle.graphics.drawCircle(0, 0, ((Math.round(Math.random() * 40)) + 20));
circle.graphics.endFill();
circle.x = theX;
circle.y = theY;
stage.addChild(circle);
var xTween:Tween = new Tween(circle, "scaleX", Bounce.easeOut, 0, 1, 0.5, true);
var yTween:Tween = new Tween(circle, "scaleY", Bounce.easeOut, 0, 1, 0.5, true);
soundChannel = pop.play();
circleReady = false;
coolDown.addEventListener(TimerEvent.TIMER_COMPLETE, timeDone);
coolDown.start();
}
function timeDone (e:TimerEvent):void
{
circleReady = true;
}
{
}
}
}
}
----------------------------------------------------------------------------------------------------------------------------------
Any help would be great, thanks
How do I make sure the compiler finds Collision.as?
Depending on the file format you are using:
1. .FLA: Collision.as has to live in the same folder as the FLA
2..XFL: Collision.as has to live one level above the .XFL file (on the same level as the folder that has the same PREFIX as your xfl)
Copy link to clipboard
Copied
if you had a physics class it should be static or a singleton. there's no need to create multiple instances of a physics class.
you would use a public method of the physics class to pass instances (like your circle) and possibly parameters (that determine the physics of the circles and anything else passed to the physics class). the physics class would update the position of objects passed to it each clock cycle.
Copy link to clipboard
Copied
I've found a collision detection script that I want to try and use in my program, but I can't get it to work. The name of the class is Collision and the function's name I need is called block, so in my Main class I use :
Collision.block (player, enemy);
And I get the error :
Access of undefined property Collision.
I've been trying for ages but I still can't get it to work, in all the examples I've seen they just use it like that. I've tried creating a variable with the block function in it, but I get weird syntax errors and other things. The Collision class is public and the function 'block' is a static public function that takes two movie clips as parameters. What am I missing?
Thanks
Copy link to clipboard
Copied
1.Make sure that the compiler finds Collision.as
2.You can only access the function the way you do if block() is a public static function
Copy link to clipboard
Copied
Yes, block() is a public static function. How do I make sure the compiler finds Collision.as? Do I have to manually specify it?
Copy link to clipboard
Copied
Let me just post the code for both of them, for reference
This is my program code :
----------------------------------------------------------------------------------------------------
package
{
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
public class Movement extends MovieClip
{
var vx:int = 3;
var vy:int = 3;
var arrowRight, arrowLeft, arrowUp, arrowDown:Boolean;
var stageWidth:int = stage.width;
var stageHeight:int = stage.height;
var enemies:Array;
public function Movement()
{
arrowRight, arrowLeft, arrowUp, arrowDown = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
addEventListener(Event.ENTER_FRAME, updating);
}
function keyPressed(event:KeyboardEvent):void
{
if (event.keyCode == 37)
{
arrowLeft = true;
}
else if (event.keyCode == 38)
{
arrowUp = true;
}
else if (event.keyCode == 39)
{
arrowRight = true;
}
else if (event.keyCode == 40)
{
arrowDown = true;
}
}
function keyReleased(event:KeyboardEvent):void
{
if (event.keyCode == 37)
{
arrowLeft = false;
}
else if (event.keyCode == 38)
{
arrowUp = false;
}
else if (event.keyCode == 39)
{
arrowRight = false;
}
else if (event.keyCode == 40)
{
arrowDown = false;
}
}
function updating (event:Event):void
{
if (arrowLeft && arrowUp)
{
player.x += -vx;
player.y += -vy;
}
else if (arrowUp && arrowRight)
{
player.x += vx;
player.y += -vy;
}
else if (arrowUp)
{
player.y += -vy;
}
else if (arrowLeft && arrowDown)
{
player.x += -vx;
player.y += vy;
}
else if (arrowDown && arrowRight)
{
player.x += vx;
player.y += vy;
}
else if (arrowDown)
{
player.y += vy;
}
else if (arrowRight)
{
player.x += vx;
}
else if (arrowLeft)
{
player.x += -vx;
}
var playerWidth:uint = player.width/2;
var playerHeight:uint = player.height/2
if (player.x + playerWidth > stage.stageWidth)
{
player.x = stage.stageWidth - playerWidth;
}
else if (player.x - playerWidth < 0)
{
player.x = 0 + playerWidth;
}
if (player.y - playerHeight < 0)
{
player.y = 0 + playerHeight;
}
else if (player.y + playerHeight > stage.stageHeight)
{
player.y = stage.stageHeight - playerHeight;
}
if (player.hitTestObject(enemy))
{
trace ("ow!");
}
Collision.block (player, enemy);
}
}
}
--------------------------------------------------------------------------------------------------------------------
And this is the code for the Collision :
--------------------------------------------------------------------------------------------------------------------
package
{
import flash.display.MovieClip;
import flash.geom.Point;
public class Collision
{
public function Collision()
{
}
//Block objects
public static function block(objectA:MovieClip, objectB:MovieClip):void
{
var objectA_Halfwidth:Number = objectA.width / 2;
var objectA_Halfheight:Number = objectA.height / 2;
var objectB_Halfwidth:Number = objectB.width / 2;
var objectB_Halfheight:Number = objectB.height / 2;
var dx:Number = objectB.x - objectA.x;
var ox:Number = objectB_Halfwidth + objectA_Halfwidth - Math.abs(dx);
if (ox > 0)
{
var dy:Number = objectA.y - objectB.y;
var oy:Number = objectB_Halfheight + objectA_Halfheight - Math.abs(dy);
if (oy > 0)
{
if (ox < oy)
{
if (dx < 0)
{
//Collision on right
oy = 0;
}
else
{
//Collision on left
oy = 0;
ox *= -1;
}
}
else
{
if (dy < 0)
{
//Collision on Top
ox = 0;
oy *= -1;
}
else
{
//Collision on Bottom
ox = 0;
}
}
//Use the calculated x and y overlaps to
//Move objectA out of the collision
objectA.x += ox;
objectA.y += oy;
}
}
}
//General purpose method for testing Axis-based collisions. Returns true or False
static public function test(objectA:Object,objectB:Object):Boolean
{
var objectA_Halfwidth=objectA.width/2;
var objectA_Halfheight=objectA.height/2;
var objectB_Halfwidth=objectB.width/2;
var objectB_Halfheight=objectB.height/2;
var dx=objectB.x-objectA.x;
var ox=objectB_Halfwidth+objectA_Halfwidth-Math.abs(dx);
if (0<ox)
{
var dy=objectA.y-objectB.y;
var oy=objectB_Halfheight+objectA_Halfheight-Math.abs(dy);
if (0<oy)
{
return true;
}
}
else
{
return false;
}
return false;
}
//Collisions between the player and platform
static public function playerAndPlatform(player:MovieClip, platform:MovieClip, bounce:Number, friction:Number):void
{
//This method requires the following getter and
//setter properties in the player object:
//objectIsOnGround:Boolean, vx:Number, vy:Number,
//bounceX:Number, bounceY:Number
//Decalre variables needed for the player's
//position and dimensions
var player_Halfwidth:Number;
var player_Halfheight:Number;
var player_X:Number;
var player_Y:Number
//Decalre variables needed for the physics calculations
var bounceX:Number;
var bounceY:Number;
var frictionX:Number;
var frictionY:Number;
//Find out whether the player object has a collisionArea
//subobject defined
if(player.collisionArea != null)
{
//If it does, find out its width and height
player_Halfwidth = player.collisionArea.width / 2;
player_Halfheight = player.collisionArea.height / 2;
//Convert the collisionArea's local x,y coordinates to global coordinates
var player_Position:Point = new Point(player.collisionArea.x, player.collisionArea.y);
player_X = player.localToGlobal(player_Position).x;
player_Y = player.localToGlobal(player_Position).y;
}
else
{
//If there's no collisionArea subobject
//Use the player's main height, width, x and y
player_Halfwidth = player.width / 2;
player_Halfheight = player.height / 2;
player_X = player.x;
player_Y = player.y;
}
//Find the platform's dimensions
var platform_Halfwidth:Number = platform.width / 2;
var platform_Halfheight:Number = platform.height / 2;
//Find the distance between the player and platfrom on the x axis
var dx:Number = platform.x - player_X;
//Find the amount of overlap on the x axis
var ox:Number = platform_Halfwidth + player_Halfwidth - Math.abs(dx);
//Check for a collision on the x axis
if (ox > 0)
{
//If the objects overlap on the x axis, a collision might be occuring
//Define the variables you need to check for a collision on the y axis
var dy:Number = player.y - platform.y;
var oy:Number = platform_Halfheight + player_Halfheight - Math.abs(dy);
//Check for a y axis collision. We know a collision must be
//occuring if there's a collision on both the x and y axis
if (oy > 0)
{
//Yes, a collision is occuring!
//Now you need to find out on which side
//of the platform it's occuring on.
if (ox < oy)
{
if (dx < 0)
{
//Collision on right
oy = 0;
dx = 1;
dy = 0
}
else
{
//Collision on left
oy = 0;
ox *= -1;
dx = -1;
dy = 0
}
}
else
{
if (dy < 0)
{
//Collision on Top
ox = 0;
oy *= -1;
dx = 0;
dy = -1;
//set the player's isOnGround property to
//true to enable jumping
player.isOnGround = true;
}
else
{
//Collision on Bottom
ox = 0;
dx = 0;
dy = 1;
}
}
//Find the direction of the collision ("dot product")
var directionOfCollision:Number = player.vx * dx + player.vy * dy;
//Calculate the new direction for the bounce ("projection")
var newDirection_X:Number = directionOfCollision * dx;
var newDirection_Y:Number = directionOfCollision * dy;
//Find the "tangent velocity":
//the speed in the direction that the object is moving.
//It's used for calculating additional platform friction.
var tangent_Vx:Number = player.vx - newDirection_X;
var tangent_Vy:Number = player.vy - newDirection_Y;
//Apply collision forces if the object is moving into a collision
if (directionOfCollision < 0)
{
//Calculate the friction
frictionX = tangent_Vx * friction;
frictionY = tangent_Vy * friction;
//Calculate the amount of bounce
bounceX = newDirection_X * bounce;
bounceY = newDirection_Y * bounce;
}
else
{
//Prevent forces from being applied if the object is
//moving out of a collision
bounceX = 0;
bounceY = 0;
frictionX = 0;
frictionY = 0;
}
//Apply platform friction
player.vx += ox - frictionX;
player.vy += oy - frictionY;
//Move the player out of the collision
player.x += ox;
player.y += oy;
//Bounce the player off the platform
player.bounceX = bounceX;
player.bounceY = bounceY;
}
}
}
}
}
--------------------------------------------------------------------------------------------------------------------
Copy link to clipboard
Copied
How do I make sure the compiler finds Collision.as?
Depending on the file format you are using:
1. .FLA: Collision.as has to live in the same folder as the FLA
2..XFL: Collision.as has to live one level above the .XFL file (on the same level as the folder that has the same PREFIX as your xfl)
Copy link to clipboard
Copied
moccamaximum wrote:
How do I make sure the compiler finds Collision.as?
Depending on the file format you are using:
1. .FLA: Collision.as has to live in the same folder as the FLA
2..XFL: Collision.as has to live one level above the .XFL file (on the same level as the folder that has the same PREFIX as your xfl)
Yessss, thank you mocca! I didn't have the Collision file in the same folder. I knew it was something small and silly .
Thanks again!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now