Copy link to clipboard
Copied
Hi there!
I have a request for some help with my project!
I am trying to produce a demonstration of how a ball will fall according to Gravity affecting the ball in this situation.
Basically, I want the user to click on the obstacle blocking the ball from rolling. If done so, the ball will start its tween.
Perhaps the ball doesn't have to be an actual tween? I'm not sure how else this could work.
However, here is the layout:
Movieclip names:
Ball: ball
Black obstacle: obstacle
Copy link to clipboard
Copied
There are a variety of ways to make this work. If you want to stick with the most basic approach then just make the ball a part of a movieclip that tweens it from where it is to where it should go. Have it stopped at frame 1 of that movieclip and when the blockade is clicked and falls away tell the ball movieclip to play.
At the other end of the spectrum, you could apply physics to the position of the ball as soon as the blockade is removed, changing the ball's x and y properties relative to time.
I would get rid of the flattened area at the top of the incline though. It looks as though the ball would not be going anywhere due to being on the flat portion rather than on the incline.
Copy link to clipboard
Copied
I am confused as to what you mean by have it stopped at frame 1 😕 Are you able to provide me with some code for telling the ball movieclip to play?
Thanks!
Copy link to clipboard
Copied
in frame 1 of the ball movieclip you would simply have: stop();
the removal of the blockade could result in a command as simple as: ball_mc.play();
Copy link to clipboard
Copied
You should check out NAPE physics (napephys.com)
It's quite simple to use and seems perfect for what you're doing.
I just made a small test which should get you going. Just go to the website, and download the SWC. Once you have it added to your movie just pasted in this code and run it. Hit the 'r' key to remove the block and cause the ball to roll down the ramp. PS - I did the test with the movie at 800x500 and 60fps
import nape.geom.Vec2;
import nape.phys.*;
import nape.shape.*;
import nape.space.Space;
import nape.util.*;
var space:Space;
var debug:Debug;
var block:Body;
var gravity:Vec2 = Vec2.weak(0, 600);
space = new Space(gravity);
debug = new BitmapDebug(stage.stageWidth, stage.stageHeight, stage.color);
addChild(debug.display);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
setUp();
function setUp():void {
var floor:Body = new Body(BodyType.STATIC);
floor.shapes.add(new Polygon(Polygon.rect(50, 450, 700, 1)));
floor.space = space;
var ramp:Body = new Body(BodyType.STATIC);
ramp.shapes.add(new Polygon([new Vec2(0,0),new Vec2(100,0), new Vec2(400,100), new Vec2(0,100), new Vec2(0,0)]));
ramp.position.setxy(10,350);
ramp.space = space;
block = new Body(BodyType.STATIC);
block.shapes.add(new Polygon([new Vec2(0,0),new Vec2(10,0), new Vec2(10,100), new Vec2(0,100), new Vec2(0,0)]));
block.position.setxy(170,300);
block.space = space;
var ball:Body = new Body(BodyType.DYNAMIC);
ball.shapes.add(new Circle(50));
ball.position.setxy(115, 220);
ball.angularVel = 0;
ball.space = space;
}
function enterFrameHandler(ev:Event):void {
space.step(1 / stage.frameRate);
debug.clear();
debug.draw(space);
debug.flush();
}
function keyDownHandler(ev:KeyboardEvent):void {
if (ev.keyCode == 82) { // 'R'
block.space = null;
}
}