Log in

View Full Version : Int round



hosdank
09-07-2008, 10:08 PM
I'm programming a simple app that has a circle in it. You can select the circle with the cursor and move it around(Or so I'm hoping to achieve one day :P). To handle a gradual acceleration PART of my code is:


function accelerate(velvar:int, maxspeed:int):int
{
if( velvar < maxspeed )
{
if(!brakes)
{
velvar += 0.5;
}
}
trace(velvar);
return velvar;
}


then later I have:

velocity = accelerate(velocity, 5);
mycirc.x += velocity;
(mycirc being the sprite)

The weird thing is that velocity never gets over 1. :P If you need anymore code from my program I'll be happy to post it. Thanks :)

Medyman
09-08-2008, 01:18 AM
What is brakes referring to? How and when is it set?

Try placing a trace within the !brakes condition and see if it's being run.

I'm assuming that you're feeding the function a value for maxspeed that's greater than 1. Is that correct? What are you feeding for the velvar value?

My guess is the trouble is with the !brakes condition. If it's true, it should be throwing an error. If there is no error, my guess is that it's not being run. You might try replacing it with if(brakes == "") or something similar.

hosdank
09-08-2008, 03:55 PM
Brakes is a boolean value that decides if the ship should start to slow down(Basically if the it get within its (destination point - velocity))
I'll try out what you said. Thanks :)

hosdank
09-08-2008, 07:07 PM
Yeah, it is getting in the if :(

Heres all the code
package spacetrade
{
import flash.display.*;
import flash.events.*;
import flash.geom.ColorTransform;
import flash.net.*;
import flash.text.*;

public class ship extends Sprite
{
internal var mycirc:Sprite = new Sprite();
private var dirx:int;
private var diry:int;
private var selected:Boolean = false;
private var regged:Boolean = false;
private var velocity:int = 0;
private var slowdown:Boolean = false;
private var stopped:Boolean = true;
private var loader:Loader; // The bitmap loader
private var brakes:Boolean=false;
private var velocity:int = 1;
//private var shiphit:String;


function ship():void
{
mycirc.graphics.beginFill(0x00FF00, 1);
mycirc.graphics.drawCircle(0,0,10);
addChildAt(mycirc,0);
mycirc.addEventListener(MouseEvent.CLICK, select);
mycirc.addEventListener(MouseEvent.MOUSE_OVER, hoveron);
mycirc.addEventListener(MouseEvent.MOUSE_OUT, hoveroff);
//mycirc.addEventListener(MouseEvent.CLICK, move);
addEventListener(MouseEvent.CLICK, move);

function select(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.CLICK, move);
mycirc.removeEventListener(MouseEvent.MOUSE_OVER, hoveron);
mycirc.removeEventListener(MouseEvent.MOUSE_OUT, hoveroff);
selected = true;
var selcolor:ColorTransform = new ColorTransform( );
selcolor.color = 0xFFFF00;
mycirc.transform.colorTransform = selcolor;
//stage.addEventListener(MouseEvent.CLICK, deselect);
e.stopPropagation();
}


function deselect(e:MouseEvent):void
{
if( selected == true )
{
mycirc.addEventListener(MouseEvent.CLICK, select);
selected = false;
var selcolor:ColorTransform = new ColorTransform( );
selcolor.color = 0x00FF00;
mycirc.transform.colorTransform = selcolor;
mycirc.addEventListener(MouseEvent.MOUSE_OVER, hoveron);
mycirc.addEventListener(MouseEvent.MOUSE_OUT, hoveroff);
}
}


function hoveron(e:MouseEvent):void
{
if(selected == false)
{
var selcolor:ColorTransform = new ColorTransform( );
selcolor.color = 0x0000FF;
mycirc.transform.colorTransform = selcolor;
stage.removeEventListener(MouseEvent.CLICK, deselect);
}
}

function accelerate(velvar:int, maxspeed:int):int
{
if( velvar < maxspeed )
{
if(!brakes)
{
velvar += .2;
trace("It got in!");
}
}
trace(velvar);
return velvar;
}


function move(e:MouseEvent):void
{
stage.frameRate=60;
var velocity:int = 1;
if(!regged)
{
addEventListener(Event.ENTER_FRAME, executemove);
regged=true;
}
function executemove(te:Event):void
{
if( e.stageX > mycirc.x )
{
velocity = accelerate(velocity, 50);
mycirc.x += velocity;
}

if( e.stageX < mycirc.x )
{
velocity = accelerate(velocity, 50);
mycirc.x -= velocity;
}

if( e.stageY > mycirc.y )
{
velocity = accelerate(velocity, 50);
mycirc.y += velocity;
}

if( e.stageY < mycirc.y )
{
velocity = accelerate(velocity, 50);
mycirc.y -= velocity;
}

if( e.stageY == mycirc.y && e.stageX < mycirc.x )
{
removeEventListener(Event.ENTER_FRAME, executemove);
regged=false;
}

if( ((e.stageY - velocity) * 10) < mycirc.y || ((e.stageX - velocity) * 10) < mycirc.x )
{
brakes=true;
velocity -= .1;
}


//if( (e.stageX - velocity) < mycirc.x )
//{
// brakes=true;
// velocity -= 1;
//}
}
}




function hoveroff(e:MouseEvent):void
{
if(selected == false)
{
var selcolor:ColorTransform = new ColorTransform( );
selcolor.color = 0x00FF00;
mycirc.transform.colorTransform = selcolor;
stage.addEventListener(MouseEvent.CLICK, deselect);
}
}
}
}
}

Medyman
09-09-2008, 02:27 AM
How are you running the function?

Your logic and coding are fine. I didn't take a very through look at it, but I don't see any glaring errors.

Try adding a trace to the beginning of the function and make sure the values are what you expect them to be.

hosdank
09-09-2008, 06:39 PM
Which function?

Medyman
09-09-2008, 08:02 PM
Which function?

accelerate()