Problems with dealing with a collision (code not working?) - actionscript-3

I am trying to detect a collision between 2 rectangles and so I am using a rectangle intersection method to determine where the collision is happening.
if (weakRect.y < strongRect.y)
{
weakRect.y -= overlapRect.height; << Does nothing
vSpeed = 0; << Changes int value
}
This line of code doesn't seem to do anything at all when I run through it, I've tried removing it and adding other stuff but it does not seem to work although other things inside the if statement work.
function rectBlock(strongRect1:MovieClip, weakRect1:MovieClip){
var strongRect:Rectangle = new Rectangle;
var weakRect:Rectangle = new Rectangle;
strongRect = strongRect1.getBounds(stage);
weakRect = weakRect1.getBounds(stage);
var overlapRect:Rectangle = strongRect.intersection(weakRect);
trace (overlapRect)
if (overlapRect.width > overlapRect.height)
{
if (weakRect.y < strongRect.y)
{
weakRect.y -= overlapRect.height;
vSpeed = 0;
}
else if (weakRect.y > strongRect.y)
{
weakRect.y += overlapRect.height;
vSpeed = 0;
}
}
if (overlapRect.width < overlapRect.height)
{
if (weakRect.x < strongRect.x)
{
weakRect.x -= overlapRect.width;
hSpeed = 0;
}
else if (weakRect.x < strongRect.x)
{
weakRect.x += overlapRect.width;
hSpeed = 0;
}
}
Even when tracing a "yes" under the if statements it prints it when I am in a collision it just does not move weakRect back out of the collision.

You need to change position of weakRect1(movieclip) not weakRect(rectangle)
weakRect1.y -= overlapRect.height; //<=>weakRect.y -= overlapRect.height;
...
weakRect1.y += overlapRect.height; //<=>weakRect.y += overlapRect.height;
...
weakRect1.x -= overlapRect.width; //<=>weakRect.x -= overlapRect.width;
...
weakRect1.x += overlapRect.width; //<=>weakRect.x += overlapRect.width;

Related

Platformer game hit test

Ok, so I have an object on the stage that moves on the "world" movieclip. I'm trying to make it so that when you're moving right. If the movieclip inside the moving movieclip("dude") called hitD collides with the walls in world, the dude stops moving forward.
Screen shots if it might help.
General stage dude object selected
http://prntscr.com/5bgjfq the world is everything but the ball
http://prntscr.com/5bgjuh
hitD
If anyone has any way they can modify these collision physics since my current code is sketchy as hell, all suggestions and ideas are welcome.
var started:Boolean;
const NUMLEVELS = 3;
var status:String;
stage.focus = stage;
if (! started)
{// Only ever do this once!
status = "falling";
started = true;
var speedX:Number = 5;
var speedY:Number = 0;
var topSpeedY:Number = 50;
var start_x:Number = dude.x;
var start_y:Number = dude.y;
var keysDown:Object = new Object();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener( Event.DEACTIVATE, appDeactivate );
dude.addEventListener(Event.ENTER_FRAME, moveDude);
var W:Number = 15;
var snows:Array = new Array();
}
for (var b:int = 0; b < 50; b++)
{
var snow:Snow = new Snow();
snows.push(snow);
addChild(snow);
}
function cleanup()
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.removeEventListener( Event.DEACTIVATE, appDeactivate );
dude.removeEventListener(Event.ENTER_FRAME, moveDude);
}
function keyIsDown(key:uint):Boolean
{
return Boolean(key in keysDown);
}
function keyPressed(e:KeyboardEvent):void
{
keysDown[e.keyCode] = true;
}
function keyReleased(e:KeyboardEvent):void
{
delete keysDown[e.keyCode];
}
function appDeactivate( event:Event ):void
{
// Get rid of all keypress info when app loses focus
keysDown=new Object();
}
function moveDude(e:Event):void
{
var obj:Object = e.target; //setting dude as object
// for now, if you get off the top of the screen you win
if (obj.y < 0)
{
cleanup();
nextScene();
return;
}
// if character dies, restart
if (obj.y > stage.stageHeight + 100)
{
gotoAndStop(1);
obj.x = start_x;
obj.y = start_y;
}
if (death!=null)
{
if (obj.hitTestObject(death))
{
trace("Dead");
}
}
if (status=="falling")
{
speedY++;
if (speedY>topSpeedY)
{
speedY = topSpeedY;
}
for (i = 0; i<2*speedY; i++)
{
obj.y++;
if (world.hitTestPoint(obj.x - obj.width / 2,obj.y,true) || world.hitTestPoint(obj.x + obj.width / 2,obj.y,true))
{
status = "ground";
break;
}
}
}
else if (status == "jumping")
{
speedY--;
for (i = 0; i<2*speedY; i++)
{
obj.y--;
if (world.hitTestPoint(obj.x - obj.width / 2,obj.y - obj.height,true) || world.hitTestPoint(obj.x + obj.width / 2,obj.y - obj.height,true))
{
speedY = 0;
break;
}
}
if (speedY==0)
{
status = "falling";
}
}
else if (status == "ground")
{
if (! world.hitTestPoint(obj.x - 8,obj.y,true) && ! world.hitTestPoint(obj.x + 8,obj.y + 4,true))
{
speedY = 0;
status = "falling";
}
if (keyIsDown(Keyboard.UP))
{
status = "jumping";
speedY = 10;
}
}
if (keyIsDown(Keyboard.DOWN)&&status=="ground")
{
dude.gotoAndStop("duck");
}
else
{
if (keyIsDown(Keyboard.SHIFT))
{
speedX = 10;
}
else
{
speedX = 5;
}
if (keyIsDown(Keyboard.LEFT))
{
for (i = 0; i<speedX; i++)
{
obj.x--;
dude.ball.rotation--; //dude.ball is a movieclip similar to dude.hitD, it spins when you move.
if (world.hitTestPoint(obj.x - obj.width / 2 + 4,obj.y - 8,true) || world.hitTestPoint(obj.x - obj.width / 2,obj.y - obj.height + 8,true))
{
dude.ball.rotation++;
obj.x++;
break;
}
}
}
else if (keyIsDown(Keyboard.RIGHT))
{
//dude.gotoAndStop("right");
//obj.scaleX = 1;
for (i = 0; i<speedX; i++)
{
obj.x++;
dude.ball.rotation++;
// The number in obj.y-4 affects the climbing ability
if (status == "ground")
{
//dude.height+= 0.1;
//dude.width += 0.1;
}//so here I'm checking if it hits the lower corner or top right corner or hitD
if (world.hitTestPoint(dude.hitD.x + obj.hitD.width/2 , obj.hitD.y,true) || world.hitTestPoint(obj.hitD.x + obj.hitD.width/2,obj.hitD.y - obj.hitD.height ,true))
//if (world.hitTestObject(obj))
{
dude.ball.rotation--;
obj.x--;
break;
}
}
}
dude.gotoAndStop(1);
}
while (status == "ground" && (world.hitTestPoint(obj.x-8, obj.y-1, true) || world.hitTestPoint(obj.x+8, obj.y-1, true)))
{
obj.y--;
}
const BORDER = 50;
var diff:int;
// Check right border:
diff = obj.x + BORDER - stage.stageWidth;
if (diff>0 && world.x>=stage.stageWidth-world.width)
{
obj.x -= diff;
world.x -= diff;
background1.x -= diff;
if (death != null)
{
death.x -= diff;
}
}
// Check left border:
diff = obj.x - BORDER;
if (diff<0 && world.x<=0)
{
obj.x -= diff;
world.x -= diff;
background1.x -= diff;
if (death != null)
{
death.x -= diff;
}
}
// Check bottom border:
diff = obj.y + BORDER - stage.stageHeight;
if (diff>0)
{
obj.y -= diff;
world.y -= diff;
background1.y -= diff;
if (death != null)
{
death.y -= diff;
}
}
// Check top border:
diff = obj.y - BORDER;
if (diff<0)
{
obj.y -= diff;
world.y -= diff;
background1.y -= diff;
if (death != null)
{
death.y -= diff;
}
}
if (obj.x > stage.stageWidth - 25)
{
if (currentFrame<NUMLEVELS)
{
gotoAndStop(currentFrame+1);
obj.x = 25;
}
else
{
obj.x = stage.stageWidth - 25;
}
}
else if (obj.x<25)
{
if (currentFrame>1)
{
gotoAndStop(currentFrame-1);
obj.x = stage.stageWidth - 25;
}
else
{
obj.x = 25;
}
}
}
Thanks in advance for any help you can provide :)
For player physics, it's more clear approach to make a central ENTER_FRAME handler function that just for calculating the transformations to be applied to player.
You can still get information from out, but you just process the final output there. Else, it could be problematic, especially when things gets more complex and when you want to add some more feature.(e.g. Imagine in the future, you wanted to add fans on the ground that blows air up, and player have to rise when on them. There you have a lot to change, and probably with many problems.
For collision detection, this function will provide you this information: to which direction(s) the player can't go with variables 'UpColl', 'DownColl', 'Right Coll' and 'LeftColl'.
Then you can refer to this information from your main function that applies the transformation to your player. I'll give example to that also below.
function collEveryFrame(event:Event):void
{
// capture player positions
player_x = WORLD.player.x;
player_y = WORLD.player.y;
// the movie clip object where you store your solid objects. Remember, ALL MovieClip objets inside this MovieClip will taken as solid objects in your game, and regardless their shape, their boundingBox will be taken as collison. So they will all be square.
collContainer = WORLD.cW;
// your player's collision object
playerColl = WORLD.player;
// RIGHT SQUARE COLLISION DETECTION
for (var i:int; i < collContainer.numChildren; i++)
{
// Check if any collision object colliding with player
if (playerColl.hitTestObject(collContainer.getChildAt(i)))
{
// One collision detected. Check 'from which side' does the player colliding with the object;
if (collContainer.getChildAt(i).y > playerColl.y + playerColl.height - p1MoveSpeed)
{
playerColl.y = collContainer.getChildAt(i).y - playerColl.height;
DownColl = true;
}
else if (collContainer.getChildAt(i).y + collContainer.getChildAt(i).height < playerColl.y + p1MoveSpeed)
{
playerColl.y = collContainer.getChildAt(i).y + collContainer.getChildAt(i).height;
UpColl = true;
}
else if (collContainer.getChildAt(i).x + collContainer.getChildAt(i).width < playerColl.x + p1MoveSpeed)
{
playerColl.x = + collContainer.getChildAt(i).x + collContainer.getChildAt(i).width;
LeftColl = true;
}
else if (collContainer.getChildAt(i).x > playerColl.x + playerColl.width - p1MoveSpeed)
{
playerColl.x = + collContainer.getChildAt(i).x - playerColl.width;
RightColl = true;
}
}
}
// RIGHT SQUARE COLLISION DETECTION [End]
}
Transformation function;
function playerMovement(event:Event):void
{
// (apply this for all sides)
// if nothing keeps player from going right;
if (! RightColl)
{
// Apply everything currently pushing the player to right
if (keyIsDown(Keyboard.RIGHT))
{
movement_Right = 15;
}else{
movement_Right = 0;
}
// example fictional wind function returns wind speed
windSpeed = getWindSpeed();
player.x += movement_Right + windSpeed + etc + etc;
// say windSpeed is -5 (Wind is coming from right, so pushing the player to left)
// so if user pressing right arrow key, player will move to right 10px for every frame, else 5px to left, etc.
}
}
In this way, everything about physics will be easy to implement.
For example, when calculating the movement to down, add gravity and jump etc.

Why dont i lag on my first chunk load but i do on my last?

I have a procedural generated game using a perlin map. Iv made it where it only loads the tiles of the area your at, and when you leave that area it deletes them and re draws them according to where you walked too. So theoretically it should only load the tiles of the area you are at. But it seems the further into my map you walk the more it begins to lag. Tho im not sure why since it should never be loading a different amount of blocks.
Here is a link to the game as of now.
http://www.fastswf.com/nzpBar0
These are the functions that are adding and deleting the tiles.
//deletes the tiles on the world.
public function deleteTiles()
{
if (tilesInWorld.length > 0)
{
for (var i:int = 0; i < tilesInWorld.length; i++)
{
worldTiles.removeChild(tilesInWorld.pop());
}
generateTile();
}
}
//generates the tiles on the world
public function generateTile()
{
for (var i:int = X/GlobalCode.MAP_SCALE; i < (X + (800/TILE_SIZE)/GlobalCode.MAP_SCALE); i++)
{
for (var j:int = Y/GlobalCode.MAP_SCALE; j < Y + (600/TILE_SIZE)/GlobalCode.MAP_SCALE; j++)
{
hm = heightmap[i][j];
if (hm >= 0.84)
{
tile = new Water();
}
else if (hm >= 0.8 && hm < 0.84)
{
tile = new Shallow();
}
else if (hm >= 0.7 && hm < 0.8)
{
tile = new Sand();
}
else if (hm >= 0.2 && hm < 0.7)
{
tile = new Tile();
}
else
{
tile = new Stone();
}
tile.width = TILE_SIZE;
tile.height = TILE_SIZE;
worldTiles.x = 0;
worldTiles.y = 0;
tile.x = TILE_SIZE * (i % 800);
tile.y = TILE_SIZE * (j % 600);
tilesInWorld.push(tile);
worldTiles.addChild(tile);
}
}
}
This is where the perlin map and the first tile area is created
public function World(parentMC:MovieClip)
{
TILE_SIZE = GlobalCode.TILE_SIZE;
map_width = GlobalCode.MAP_WIDTH;
map_height = GlobalCode.MAP_HEIGHT;
pmap = new BitmapData(map_width,map_height);
grid_width = new uint(map_width / TILE_SIZE);
grid_height = new uint(map_height / TILE_SIZE);
//map_width = GlobalCode.MAP_WIDTH;
//map_height = GlobalCode.MAP_HEIGHT;
pmap.perlinNoise(map_width,map_height, 6, _seed, true, false, 1, true);
for (var i:uint=0; i < grid_width; i++)
{
heightmap[i] = new Array();
for (var j:uint=0; j < grid_height; j++)
{
heightmap[i][j] = new uint();
}
}
//Divide the map in to a 7x7 grid and take data at each interval
for (i = 0; i < grid_width; i++)
{
for (j = 0; j < grid_height; j++)
{
pixelPoint.x = Math.round((i/grid_width) * pmap.width)+1;
pixelPoint.y = Math.round((j/grid_width) * pmap.height)+1;
heightmap[i][j] = pmap.getPixel(pixelPoint.x,pixelPoint.y);
heightmap[i][j] /= 0xffffff;
if (heightmap[i][j] < darkest_pixel)
{
darkest_pixel = heightmap[i][j];
}
}
}
//Adjust values to a min of 0
for (i = 0; i < grid_width; i++)
{
for (j = 0; j < grid_height; j++)
{
heightmap[i][j] -= darkest_pixel;
if (heightmap[i][j] > brightest_pixel)
{
brightest_pixel = heightmap[i][j];
}
}
}
//Adjust values to highest value of 1
for (i = 0; i < grid_width; i++)
{
for (j = 0; j < grid_height; j++)
{
heightmap[i][j] /= brightest_pixel;
}
}
worldTiles = new Sprite();
parentMC.addChild(worldTiles);
generateTile();
}
this is what creats the scroll rect and the X/Y changes when you walk to the edge of a screen.
public function update(e:Event)
{
world.worldTiles.scrollRect = new Rectangle(X,Y,800,600);
if (canMove == true)
{
MovePlayer();
}
player.update();
PlayerOnTile();
}
And for giggles this is what moves my character and the scroll/rect
protected function MovePlayer()
{
if (goin[0] == 1)
{
player.y -= moveSpeed;
if (player.y <= 0 && (Yloc) > 0)
{
world.Y -= int(600/world.TILE_SIZE)/MAP_SCALE;
world.deleteTiles();
Y -= 600 / MAP_SCALE;
Yloc -= 1;
player.y += 600;
}
}
if (goin[1] == 1)
{
player.y += moveSpeed;
if (player.y >= 600 && (Yloc + 1) < MAP_SCALE)
{
world.Y += int(600/world.TILE_SIZE)/MAP_SCALE;
world.deleteTiles();
Y += 600 / MAP_SCALE;
Yloc += 1;
player.y -= 600;
}
//world.worldTiles.y -= moveSpeed;
}
if (goin[2] == 1)
{
player.x -= moveSpeed;
if (player.x <= 0 && (Xloc) > 0 )
{
world.X -= int(800/world.TILE_SIZE)/MAP_SCALE;
world.deleteTiles();
X -= 800 / MAP_SCALE;
Xloc -= 1;
player.x += 800;
}
//world.worldTiles.x += moveSpeed;
}
if (goin[3] == 1)
{
player.x += moveSpeed;
if (player.x >= 800&& (Xloc + 1) < MAP_SCALE)
{
world.X += int(800/world.TILE_SIZE)/MAP_SCALE;
world.deleteTiles();
X += 800 / MAP_SCALE;
Xloc += 1;
player.x -= 800;
}
//world.worldTiles.x -= moveSpeed;
}
}
It's very simple and basic. You are creating new objects constantly and after a while your app can't get enough memory to create new object and also needs to use big chunk of CPU power to clean up. All this produces lag and is a form of memory leak. You need to reuse object instead of creating new ones. This process is called Object Pooling. Once a graphic is not needed, instead of discarding it and creating a new one next time, you keep it and reuse it next time. That way you don't need additional memory and your app doesn't need more and doesn't need to clean up as much (called garbage collection).
public function deleteTiles()
{
if (tilesInWorld.length)
{
for (var i:int = 0; i < tilesInWorld.length; i++)
{
worldTiles.removeChild(tilesInWorld[i]);//no new object creation when deleting
//tilesInWorld.pop() create a new array internally
}
tilesInWorld.length = 0;//empty array
generateTile();//this should be improved too
}
}
if worldTiles holds only titles then worldTiles.removeChildren() will do it as:
worldTiles.removeChildren();
tilesInWorld.length = 0;
generateTile();
I don't remember how Actionscript handles it, but it might be your for loop in the deleteTiles() function. It only continues until i < tilesInWorld.length, and every iteration of the for loop your i increases by one, and your tilesInWorld decreases by one. This means that you'd only be deleting half of the tiles every time you try to remove all the tiles.
Try using a while loop instead, and see if that fixes it. e.g.
public function deleteTiles()
{
if (tilesInWorld.length > 0)
{
while (tilesInWorld.length > 0)
{
worldTiles.removeChild(tilesInWorld.pop());
}
generateTile();
}
}

Moving objects on the stage

Let's say I have 5 objects in an array and I move all them along x-axis like this:
vx = 5;
for (i:int = 0; i < objects.length; i++)
{
objects[i].x += vx;
}
I would like to make this.
If any object from array 'objects' hit PointA, move all objects from that array to left side, for example set vx *= -1;
I can make only this:
for (i:int = 0; i < objects.length; i++)
{
// move right
objects[i].x += vx;
if (objects[i].hitTest(PointA))
{
// move left
vx *= -1;
}
}
This will change object's direction, but I need to wait all objets hit PointA.
How to change the direction of all objects in array, if any of them hit PointA?
I don't know action script but you should set a boolean outside the for loop, like bGoingRight
Check that this is true and move objects right, if its false move the objects left. When you pass the hitTest you should changed the boolean to false.
Rough Example
var bGoRight:Boolean = true;
for (i:int = 0; i < objects.length; i++)
{
if(bGoRight)
{
// move right
objects[i].x += vx;
}
else
{
// move left
vx *= -1;
}
if (objects[i].hitTest(PointA))
{
// if any object hit the point, set the flag to move left
bGoRight = false;
}
}
So you'll need to check the objects that have already hit PointA, store them, then check if the updated storage count is equivalent to your objects array. Then when that case is satisfied you can change the vx variable. This could look something like this:
//set a variable for storing collided object references
//note: if you are doing this in the IDE remove the 'private' attribute
private var _contactList:Array = [];
for (i:int = 0; i < objects.length; i++)
{
// move right
objects[i].x += vx;
if (objects[i].hitTest(PointA))
{
if( contactList.length == objects.length ) {
// move left
vx *= -1;
//clear our contact list
contactList.length = 0;
}
else if ( noContact( objects[i] ) ) {
contactList.push( objects[i] );
}
}
}
Then the function in the else if statement noContact(), again if you are doing this in the IDE you will need to remove the private attribute.
private function noContact( obj:* ):Boolean {
if ( contactList.indexOf( obj ) != -1 ) {
return false;
}
return true;
}
Another way you can do this is like this (a boolean way as stated in the other answer), but is reliant on your storage setup correct:
//set this variable for directionRight (boolean)
private var directionRight:Boolean = true;
for (i:int = 0; i < objects.length; i++)
{
// move right
objects[i].x += vx;
if (objects[i].hitTest(PointA))
{
//we are moving to the right, object[i] is the last object
if( i == objects.length-1 && directionRight ) {
// move left
directionRight = !directionRight;
vx *= -1;
}
else if ( i == 0 && !directionRight ) ) {
// move right
directionRight = !directionRight;
vx *= -1;
}
}
}

Error with jump function when putting script on movieclip

I'm having a problem with my jump function for a platformer engine. I've moved all of the hit testing onto the individual platforms in the level, so I don't have to write things out for each individual one. The problem with this is that only the first movieclip added to the stage can be jumped on to. The others just make the player sink through the floor if up is held down (as they should- as he's not meant to be there there's nothing to stop him).
So the problem is that the check for the jump just isn't registering properly on the second block. Here's the code (sorry about the excessive MovieClip(parent) s)
var playerRef:MovieClip = MovieClip(parent).player;
stage.addEventListener(Event.ENTER_FRAME, hitUpdate);
function hitUpdate(e:Event):void {
if (playerRef.hitTestPoint(playerRef.x,this.y - (playerRef.height/2)) && playerRef.x + playerRef.width > this.x && playerRef.x < this.x + this.width && !MovieClip(parent).upDown) {
MovieClip(parent).downMomentum = 0;
playerRef.y = this.y - playerRef.height;
}
if (playerRef.hitTestPoint(playerRef.x,this.y + this.height) && playerRef.x + playerRef.width > this.x && playerRef.x < this.x + this.width) {
MovieClip(parent).downMomentum = 0;
playerRef.y = this.y + this.height + MovieClip(parent).gravity;
}
if (playerRef.hitTestPoint(this.x,playerRef.y) && playerRef.y + playerRef.height > this.y && playerRef.y < this.y + this.height) {
MovieClip(parent).speed = 0;
playerRef.x = this.x - playerRef.width;
}
if (playerRef.hitTestPoint(this.x + this.width,playerRef.y) && playerRef.y + playerRef.height > this.y && playerRef.y < this.y + this.height) {
MovieClip(parent).speed = 0;
playerRef.x = this.x + this.width;
}
if (playerRef.hitTestObject(this)) {
MovieClip(parent).groundTouch = true;
} else {
MovieClip(parent).groundTouch = false;
}
}
And the jump function runs like this:
stage.addEventListener(Event.ENTER_FRAME, updates);
function updates(e:Event):void{
player.y += downMomentum;
if(!groundTouch && downMomentum < downMomCap){
downMomentum += gravity;
}
if(downMomentum > downMomCap){
downMomentum = downMomCap;
}
if(upDown && !jumping && groundTouch){
jumping = true;
jumpTimer.reset();
jumpTimer.start();
}
}
jumpTimer.addEventListener(TimerEvent.TIMER, jumpTick);
function jumpTick(e:TimerEvent):void{
if(downMomentum*-1 < downMomCap){
downMomentum -= jumpProg * jumpIncrement;
}else{
downMomentum = downMomCap * -1;
}
jumpProg -= 1;
}
jumpTimer.addEventListener(TimerEvent.TIMER_COMPLETE, jumpDone);
function jumpDone(e:TimerEvent):void{
jumpTimer.stop();
jumping = false;
jumpProg = 5;
}
I just can't find a solution to this, though I suspect it's something to do with how it checks for the player touching the platform.

Constrain movement of HSlider Thumb by Accelerometer

I'm moving an HSlider thumb via the accelerometer. The following code works fine. The problem, though, is that as I keep tilting the device, xSpeed continues to increment. This means that when I now tilt it the other way, the thumb doesn't move for a while -- since, depending on how long I was holding tilted in that intial direction, xSpeed has been going up and up.
So this works, but with the abovementioned flaw:
private function readAcc(e:AccelerometerEvent):void
{
xSpeed -= e.accelerationX * 4;
myHSlider.dispatchEvent(new FlexEvent("valueCommit"));
myHSlider.value += xSpeed;
}
But what I want to do is to stop incrementing xSpeed once the hSlider.value == either the minimum or the maximum. Sounds simple, but when I put in if statements, they prevent the thumb from moving at all:
private function readA(e:AccelerometerEvent):void
{
if(h.minimum < h.value && h.maximum > h.value)
{
xSpeed -= e.accelerationX * 4;
h.dispatchEvent(new FlexEvent("valueCommit"));
h.value += xSpeed;
var lastSpeed:Number = xSpeed;
}
else if (h.value == h.minimum || h.value == h.maximum)
{
xSpeed = lastSpeed;
h.dispatchEvent(new FlexEvent("valueCommit"));
h.value += xSpeed;
}
}
What should the logic be to make this work?
Thanks.
Perhaps your slider value is exceeding the min or max limits when you add (or subtract) xSpeed. Check for this, and subtract if the value is too high, or add if the value is too low.
// EDIT: Code modified to reflect most recent comment
if(h.minimum < h.value && h.maximum > h.value)
{
xSpeed -= e.accelerationX * 4;
var lastSpeed:Number = xSpeed;
}
else if (h.value <= h.minimum )
{
xSpeed = 2;
}
else if ( h.value >= h.maximum )
{
xSpeed = -2
}
h.value += xSpeed;
h.dispatchEvent(new FlexEvent("valueCommit"));
OK, have figured this out and wanted to share it. The key is not to mess with setting the hslider's h.value. Rather, just set the x of the thumb, and let the value take care of itself. The accelerometer event calls a handler; in that handler create a moveThumb() function which will adjust that x. Then, from within the moveThumb() function, dispatch the valueCommit event so that the hslider will respond.
private var xSpeed:Number;
private function accUpdateHandler(e:AccelerometerEvent):void
{
xSpeed -= e.accelerationX;
moveThumb();
}
private function moveThumb():void
{
var newX:Number = h.thumb.x + xSpeed;
var newY:Number = h.thumb.y + ySpeed;
if (newX < 0)
{
h.thumb.x = 0;
xSpeed = 0;
}
else if (newX > h.width - h.thumb.width)
{
h.thumb.x = h.width - h.thumb.width;
xSpeed = 0;
}
else
{
h.value += xSpeed;
}
h.dispatchEvent(new FlexEvent("valueCommit"));
}