SRL Archive - Java Help and Tutorials

ActionScript Help.


mrpickle

2010-08-19 16:30:15

So, I've started AS3 just about 1 month ago.

I want the user to be able to click and hold down the mouse anywhere on the screen, and a ball will appear. As the user keeps holding down the ball will grow bigger without moving. As the ball reaches max size, it will simply hold that position.

Ball can drop anytime after it has been created, but only when the user lets go of the mouse button.

Source DL: http://www.mediafire.com/?xvd24wjk2zb3zsg
Compiled DL:http://www.mediafire.com/?1hdafc7wrvtb1md

//mrpickle needz help! It auto drops ball when reach max size. + it drops really fast (only @ max size)

// You'll have to manually create and linkage 2 movieClips named (Boulder, RandomSymbol) to make it work
import flash.display.Sprite;
import flash.events.Event;
import flash.display.IBitmapDrawable;
import flash.text.TextField;


var mainBall:Boulder = new Boulder();
var rS:RandomSymbol = new RandomSymbol();

var rSizeNow,mX,mY,gravUpdate:int = 0;

const maxBoulderSizeNow = 55;
const gravity = 9.8;



stage.addEventListener(MouseEvent.MOUSE_DOWN, createBall);
stage.addEventListener(MouseEvent.MOUSE_UP,dropBoulder);



function createBall(event:MouseEvent):void
{
trace("1");
//
mX = mouseX;
mY = mouseY;

rS.x = mX;
rS.y = mY;
addChild(rS);
stage.addEventListener(Event.ENTER_FRAME,doCreateBall);

}

function doCreateBall(event:Event):void
{
trace("2");
if (rSizeNow <= maxBoulderSizeNow)
{
rSizeNow = rSizeNow + 1;
mainBall.x = mX;
mainBall.y = mY;
mainBall.width = 40 + rSizeNow;
mainBall.height = 40 + rSizeNow;
addChild(mainBall);
}
else if (rSizeNow > maxBoulderSizeNow)
{
addChild(mainBall);
}
}

function dropBoulder(event:MouseEvent):void
{

stage.removeEventListener(Event.ENTER_FRAME,doCreateBall);
rSizeNow = 0;

stage.addEventListener(Event.ENTER_FRAME,doDropBoulder);
gravUpdate = 0;
}

function doDropBoulder(event:Event):void
{
trace("4");
gravUpdate = gravUpdate + (gravity^2)/10;
mainBall.y = mainBall.y + gravUpdate;
}
//}

So the problem I have is that the Ball drops automatically when max size is reached when there is NO code that tells it to. I tried debugging with tracing, and it actually holds at the Max size for split second before just dropping.

Yes, I do prefer to ask here rather than the actual AS sites xD.

i luffs yeww

2010-08-19 16:36:34

I don't really know anything about ActionScript at all, but in dropBoulder or doDropBoulder couldn't you check to see if the mouse is down? If it is, then the ball doesn't drop. If it isn't, the ball drops.

(Can't really read through the code atm, but I think a simple if would work?)

Frement

2010-08-19 16:39:34

When there is no code inside the functions, it should not drop, hes asking why it does.

mrpickle

2010-08-19 16:39:42

I've already tried all the basic methods… The whole function only works if mouse is down. But frement got it :).

To zero in on the problem…


stage.addEventListener(MouseEvent.MOUSE_DOWN, createBall);
...
// This function is attached to another that detects mouse down.
function doCreateBall(event:Event):void
{
trace("2");
if (rSizeNow <= maxBoulderSizeNow)
{
rSizeNow = rSizeNow + 1;
mainBall.x = mX;
mainBall.y = mY;
mainBall.width = 40 + rSizeNow;
mainBall.height = 40 + rSizeNow;
addChild(mainBall);
}
else if (rSizeNow > maxBoulderSizeNow)
{
addChild(mainBall);
}
}

I dont see anyway for this ending prematurely (thought not most efficient)… it just goes through the loop, and adds the image over again if @ max size.

mrpickle

2010-08-20 15:08:27

after hours of just fussing around, rewritting, and looking @ other examples of "basic shooter games", I just changed the above function to:

function doCreateBall(event:Event):void
{
trace("2");
if (rSizeNow <= maxBoulderSizeNow)
{
rSizeNow = rSizeNow + 1;
mainBall.x = mX;
mainBall.y = mY;
mainBall.width = 40 + rSizeNow;
mainBall.height = 40 + rSizeNow;
addChild(mainBall);
}
else if (rSizeNow > maxBoulderSizeNow)
{
mainBall.x = mX;
mainBall.y = mY;
mainBall.width = 40 + rSizeNow;
mainBall.height = 40 + rSizeNow;
}
}

I basically told the script in 2 places to not move. Whereas before, I told it to increase size as user holds down mouse. Then when max size is reached, it is programmed to do nothing… but of course, it's there that the ball randomly decides to drop. I still dunno why this problem occured, but that was a fix.