ForumsProgramming ForumBox Physics

12 3190
ShadowVoxx
offline
ShadowVoxx
12 posts
Nomad

I was wondering how to make two boxes, lets say, fall on to each other and then fall off if the movie clip get removed.
Ive already got the gravity working.
any help would be much apreciated. Im looking for eather code or a tutorial.
im AS2
im not looking for an engine just something like âMusic in Motionâ
Heres my code

for falling box
&ltre>
class Box extends MovieClip {
var velocity = 0;
var gravity = .5;
var friction = .01;
function onLoad() {
this._x = Math.random()*600 + 50;
this._y = -100;
}
function onEnterFrame() {
velocity += gravity;
velocity -= friction*velocity;
_y += velocity;
if (_y>_root.floor._y) {
_y = _root.floor._y;
velocity *= -.4;
gotoAndPlay("BoxDie"
}
}
}
</pre>
and for my hero
&ltre>
class hero extends MovieClip {
var velocity = 0;
var gravity = 1;
var friction = .01;
var jumping = false;
var jump = 5;
var boxTimer = 0;
function onEnterFrame() {


boxTimer += 1;

if (boxTimer>20) {
boxTimer = 0;
_root.attachMovie("Box","Box"+_root.getNextHighestDepth(),_root.getNextHighestDepth());
}
if (Key.isDown(Key.LEFT)) {
this._x -= 10;
this._xscale = 100;
}
if (Key.isDown(Key.RIGHT)) {
this._x += 10;
this._xscale = -100;

}
if (Key.isDown(Key.UP) && jumping == true) {
velocity -= 20;
jumping = false;

}

velocity += gravity;
velocity -= friction*velocity;
_y += velocity;


if (_y>_root.floor._y) {
_y = _root.floor._y;

velocity *= -.01;
jumping = true;

}
if (this._x>650) {
this._x = 0;
} else if (this._x<0) {
this._x = 650;
}
if (this.hitTest(_root.Box.heroKiller)) {
Die;
}
}
}
</pre>

  • 12 Replies
Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

You could code directly inside the Box movieclip so it's always falling, except if there's something under it

ShadowVoxx
offline
ShadowVoxx
12 posts
Nomad

thats what it does
it stops at the floor but on ly on its ypos not the movieclip itself

Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

Could you detail some more what you wanna do and what is happening? I don't think I get it

ShadowVoxx
offline
ShadowVoxx
12 posts
Nomad

the hero and box fall but when the heros/Boxes ypos is lower then the floors ypos it makes it the same

Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

And what part isn't working?

ShadowVoxx
offline
ShadowVoxx
12 posts
Nomad

no it works i just need a better way

somthing like if(this hits floor){
falling=false
}

ShadowVoxx
offline
ShadowVoxx
12 posts
Nomad

im new so dont know how to edit post
found problem fixing
and hopefully itll stay fixed
but if there are any comments please keep saying them

ShadowVoxx
offline
ShadowVoxx
12 posts
Nomad

na fix doesnt work back to ideas
so whos got any

ShadowVoxx
offline
ShadowVoxx
12 posts
Nomad

ok updated code

&ltre>
class hero extends MovieClip {
var velocity = 0;
var gravity = 1;
var friction = .01;
var jumping = false;
var jump = 5;
var boxTimer = 0;
var falling:Boolean;
function onEnterFrame() {


boxTimer += 1;

if (boxTimer>20) {
boxTimer = 0;
_root.attachMovie("Box","Box"+_root.getNextHighestDepth(),_root.getNextHighestDepth());
}
if (Key.isDown(Key.LEFT)) {
this._x -= 10;
this._xscale = 100;
}
if (Key.isDown(Key.RIGHT)) {
this._x += 10;
this._xscale = -100;
}
if (Key.isDown(Key.UP) && jumping == true) {
velocity -= 20;
jumping = false;
}
velocity += gravity;
velocity -= friction*velocity;
if (falling == true) {
_y += velocity;
}
if (hero.hitTest_floor.hitTest(_root.floor)) {

jumping = true;
falling = false;
} else{
falling = true
}
if (this._x>650) {
this._x = 0;
} else if (this._x<0) {
this._x = 650;
}

}
}

</pre>

Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

You want to fix the drawback it makes from when it goes past the floor and then it goes back up to the Y limit, or you just want a better code?

ShadowVoxx
offline
ShadowVoxx
12 posts
Nomad

better code but implanted with what ive got

ShadowVoxx
offline
ShadowVoxx
12 posts
Nomad

this is the file if any one can help can you please fix it then upload it
[url]http://uploading.com/files/JC3P3Q4G/hero.zip.html[/url]

Showing 1-12 of 12