I want that my spaceship gets louder when my mouse moves away from it (and vice versa). Problem is that my function produces an annoying crackling sound I can't get rid of when I move my mouse away from it (and vice versa). The sound file is ok so far. What can I try next?
shipSpeed = (abs(player.x - playerPrevPt.x) + abs(player.y - playerPrevPt.y)) / 2;//the ship's speed gets calculated
//this is used to determine whether the ship's volume of its sound shall be increased or decreased
if (shipSpeedPrev < shipSpeed) {
sndManager.increaseSound(Main.SOUND_BACKGROUND, false, .01);
} else if (shipSpeedPrev > shipSpeed) {
sndManager.decreaseSound(Main.SOUND_BACKGROUND, false, .01);
} else if (shipSpeedPrev == shipSpeed) {
} else {
}
shipSpeedPrev = (abs(player.x - playerPrevPt.x) + abs(player.y - playerPrevPt.y)) / 2;//this is another ship speed so I can compare them
//the decreaseSound-function is almost the same
public function increaseSound(soundName:String, isSoundTrack:Boolean = false, steps:Number = .1, targetVol:Number = 1):void {
if (isSoundTrack) {
if (soundTrackChannel != null) {
musicVolumeAdj.volume += steps;
if (musicVolumeAdj.volume >= targetVol) {
musicVolumeAdj.volume = targetVol;
}
soundTrackChannel.soundTransform = musicVolumeAdj;
}
} else {
soundVolumeAdj = new SoundTransform(incrSndVal, 0);
incrSndVal += steps;
soundVolumeAdj.volume += incrSndVal;
if (soundVolumeAdj.volume >= 1) {
soundVolumeAdj.volume = 1;
}
soundChannels[soundName].soundTransform = soundVolumeAdj;
}
}
Most likely the audio ends up being clipped when you increase the volume. Try to find out at what volume the clipping occurs, and then change your function so that it doesn't got beyond that volume.
Perhaps you could also re-author the audio file and reduce the overall volume, so that you can increase it more from your game.
Related
Hey everyone so I've been at this for awhile now and finally decided to ask for some help. So I am creating a game in AS3 where the object rotates in a circular motion from either left to right depending on the users mouse Presses. So I have some variables set up to act as friction. What I AM trying to accomplish is when the object is greater or less than a certain rotation degree I want the object to feel like it being pulled more and more to that side that it is currently at and the only way the object can say come back to its original position is if the use clicks on the mouse enough so their is no more force acting on it and say the speed increases at the same time for difficulty.
Here are the Variables I am currently working with:
//Variables
speed = 0.2;
vx = 0;
friction = 0.93;
maxspeed = 10;
I also have these buttons on stage so the user can click them to change the rotation of the objectlike so:
mainScreen.leftBtn.addEventListener(MouseEvent.CLICK, leftButtonClicked);
mainScreen.rightBtn.addEventListener(MouseEvent.CLICK, rightButtonClicked);
private function leftButtonClicked(e:MouseEvent):void
{
clickLeft = true;
clickRight = false;
}
private function rightButtonClicked(e:MouseEvent):void
{
clickRight = true;
clickLeft = false;
}
and I try to set up the mechanics in my ENTER_FRAME event listener like so:
//RIGHT = CLOCKWISE +, Left = COUNTER CLOCKWISE -
if (clickRight)
{
vx += speed;
moveSlow = true;
moveFast = false;
}else
if (clickLeft)
{
vx -= speed;
moveSlow = true;
moveFast = false;
}else
{
vx *= friction;
}
//IF object is Past 15 Degrees make object go faster MOVE FAST
if (object.rotation > 15)
{
moveFast = true;
moveSlow = false;
trace("MOVE_FAST");
}else
if (object.rotation < - 15)
{
moveFast = true;
moveSlow = false;
}else
{
vx *= friction;
}
object.rotation += vx;
//lumberJack.rotation += speed;
//boundaries of object
if (vx > maxspeed)
vx = maxspeed;
else if (vx < -maxspeed)
vx = -maxspeed;
I know I need to add something in the if object.rotation statement but not to sure what i also know i need to add something in the Mouse clicked event listeners to manipulate either speed or friction so the user can pull away from the force acting on it. I tried several time but still cant seem to figure it out. As of now the object rotates left or right depending on the users input and say the object is moving left and the user presses right the object slowly moves back to the left then returns to normal speed.
Please If anyone can help me figure this out I will greatly appreciate it!
To be honest I am having a little bit of trouble following your question, so this may only be a partial answer. I know, bad me, but I don't have enough stupid internet points to leave a comment, so this is all I can do to help, and I just can't care about the internet point system anymore.
First, for the love of insert deity, clean up all that unnecessary white space.
Here's a potential problem in your code: the rotation property of display objects returns a value between -180 and 180. This means that every 180 degrees, the rotation value changes sign. So you can't use whether the rotation is positive or negative to determine which direction the object is rotating in. That should be stored in a separate variable.
Another thing to consider: if moveSlow and moveFast are never true at the same time, you don't need to have two variables because it's redundant. You don't make use of those variables in your code above, but assuming you wrote this:
if (moveSlow) {
moveALittleBit();
} else if (moveFast) {
moveALot();
}
You could replace it with:
if (moveSlow) {
moveALittleBit();
} else {
moveALot();
}
Not only is that giving yourself unnecessary work, but it's bad because it means you can create "invalid states" (i.e. if you make a mistake and "moveSlow" and "moveFast" are both true at the same time).
Likewise, you shouldn't need separate variables for "clickedLeft" and "clickedRight" if they are both mutually exclusive. If they can both be false at the same time, however, you might be better off with something like:
clickDirection = "left";
clickDirection = "right";
clickDirection = "none";
If you went that route, you'd be better off using string constants instead of hardcoded strings, but I think that's getting too off-topic.
After reading your question many times, it sounds like maybe what you are looking for is momentum. Does this cover what you need?
if (clickRight)
{
vx += speed;
momentum = 0;
} else if (clickLeft)
{
vx -= speed;
momentum = 0;
} else
{
vx *= friction;
}
//IF object is Past 15 Degrees make object go faster MOVE FAST
if (object.degreesRotated > 15)
{
momentum += 1;
}else if (object.degreesRotated < - 15)
{
momentum -= 1;
}else
{
vx *= friction;
}
vx += momentum;
object.rotation += vx;
object.degreesRotated += vx; //remember how much we've rotated
//lumberJack.rotation += speed;
If object is a dynamic type, you can add the "degreesRotated" property whenever you feel like it. Otherwise, you might have to make a new class by extending whatever display type that object is, and add the degreesRotated field to that class
I have an flash game with the following code (http://pastie.org/9248528)
When I run it, the player just falls and doesn't stop when he hits a platform.
I tried debugging it and I had an error with moveCharacter's timer, but I don't know if that is the main problem.
I put the player inside the wall and debugged it using breakpoints and it didn't detect that the player was inside the wall, skipping moving it to outside of the wall.
Anyone has any ideas on what is wrong with my code?
The problem is in this code:
// Check if character falls off any platform
for (var i:int = 0; i < platform.length; i++) {
if (player.x < platform[i].x || player.x > platform[i].x + platform[i].width) {
onPlatform = false;
}
}
Since the player cannot simultaneously be on every platform at once, his x position is pretty much guaranteed to be out of bounds of at least 1 platform, which will set onPlatform to false. Instead you would need to keep a reference to which platform the player is on, like so:
var lastPlatform:Sprite; //holds reference to last platform player was on
// Function to move character
function moveCharacter(evt:TimerEvent):void {
....
// Check if character falls off the platform he was last on
if (lastPlatform != null && (player.x < lastPlatform.x || player.x > lastPlatform.x + lastPlatform.width)) {
onPlatform = false;
}
}
function detectCollisions():void {
// Check for collisions with platforms
for (var p:int = 0; p < platform.length; p++) {
// Adjust character to platform level if with landing depth of the platform
if (!onPlatform && player.hitTestObject(platform[p]) && lastPosY < platform[p].y) {
lastPlatform = platform[p]; //save reference
player.y = platform[p].y;
jumping = false;
onPlatform = true;
dy = 0;
// Prevent character from dropping sideways into platforms
} else if (!onPlatform && player.hitTestObject(platform[p])) {
player.x = lastPosX;
}
}
.....
}
This should work better, though it is still not the most object-oriented way to do this. Hope this helps!
I am trying to code a script in which a movieclip drops a rope and catches fishes that follows it up if it touches it. here is the issue , i am using hitTestObject to detect collision . Ofcourse the problem is that i trigger the function when it touches but as soon as it doesnt touch the function for moving the movie starts so basically the fish goes up for few seconds and then starts moving straight again .
To try and fix that i tried to make a boolean variable which changes to true or false according to hit and accordingly makes the movieclip moves but also doesnt work because as soon as one mc is not touching the other it changes from true to false or 1 to 0 ..tried both (as in with boolean variable and Number variable) . Any help or putting me on the right direction would be highly appreciated . Thank you so much
// fish capture code
this.addEventListener(Event.ENTER_FRAME,handleCollision);
function handleCollision(e:Event):void
{
if (ropeHit.hitTestObject(fishy_1_a))
{
stopFish1();
trace(movefish1);
}
else
{
moveFish1();
}
}
//code enemy fishy
//fish 1 A
function moveFish1()
{
if (fishy_1_a.x < 800)
{
fishy_1_a.x += xSpeed;
}
else if (fishy_1_a.x >= 800)
{
fishy_1_a.x = -100;
}
}
function stopFish1()
{
fishy_1_a.y -= xSpeed;
}
Define some flag, that you can test:
function handleCollision(e:Event):void {
//Check if fishy is caught
if (!fishy_1_a.catched && ropeHit.hitTestObject(fishy_1_a)) {
//Change flag
fishy_1_a.catched = true;
trace("Gotcha!");
}
if (fishy_1_a.catched) {
stopFish1();
}else {
moveFish1();
}
}
Im trying to random an object(2) every time object(1).y is beyond the stage. but the problem comes that its constantly moving and sometimes its "jumps" that position where im looking to make the change.
i did try with the "if (road1.y >= stage.stageHeight) {" but it doesnt trigger.
And when I'm doing it the the speed of the movement it triggers only when it has been on the stage 2 times before.
the registration point of all MovieClips are in the TOP LEFT.
the code is this
private var road1,road4:Road1;
private var road2:Road2;
private var road3:Road3;
private var randomRoad:Sprite = new Sprite();
private var offset:Number = 0;
private var counter:Number = 0;
public function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE,onAdded);
addChild(road1=new Road1());
addChild(randomRoad);
addChild(road4=new Road1());
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(e:Event):void {
if (startRandom == true) {
if (Math.random() > 0.5) {
randomRoad.addChild(road2 =new Road2());
} else {
randomRoad.addChild(road3 =new Road3());
}
startRandom = false;
trace(randomRoad);
trace(startRandom);
}
if (road1.y >= stage.stageHeight) {
startRandom = true;
trace("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
}
offset += speed;
counter++;
road1.y = offset % 800 - 800;
randomRoad.y = road1.y + road1.height;
road4.y = randomRoad.y + randomRoad.height;
}
Try this:
if (road1.y >= -speed) {
startRandom = true;
trace("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
}
What is the height of the stage? 800? Maybe the problem is in that road1.y will never go greater than -1;
Wouldn't it be more logical to base your spawning of an object on distance traveled? Then it wouldn't be reliant on determining the y position of a given road piece in relation to the screen.
I am going to assume you have a car in this game, since there is road. Give the car a distance variable and increment that variable based on the speed.
distance += speed;
if (distance > 400)
{
spawnObject();
distance = 0; // reset the distance traveled.
}
EDIT :
I think I may have misunderstood what you were trying to do as I thought you were trying to spawn objects on the side of the road and this was a method of determining when to spawn them. In re-reading it, it seems like the 'object' you are trying to spawn is the next road piece itself. Would have been better to just use the word road in your question as opposed to 'object(2)'
I'm building a scrollbar.
On MouseDown I start a repeating timer to position the Scrollbar-Button(Slider).
When it reaches minimum/maximum it jitters (switching between min/max and stage.mouseY…)
How can I prevent that?
private function onTime(e:TimerEvent):void
{
if(this._scrollBtn.y < min)
{
this._scrollBtn.y = min;
}
else if(this._scrollBtn.y > max-this._scrollBtn.height)
{
this._scrollBtn.y = max-this._scrollBtn.height;
}
else
{
this._scrollBtn.y = stage.mouseY;
}
}
I'd recommend listening for the MouseEvent.MOUSE_MOVE instead of using a timer, that way you're only doing work if and when the mouse is moving.
Your problem is likely that your validating the position of the scroll handle "after" moving it, letting it be out of bounds for a while before the next update comes and moves it back.
There's also no reason in having this everywhere, unless it's explicitly needed.
private function onTime(e:TimerEvent):void
{
_scrollBtn.y = stage.mouseY;
if(_scrollBtn.y < min)
{
_scrollBtn.y = min;
}
else if(_scrollBtn.y > max - _scrollBtn.height)
{
_scrollBtn.y = max - _scrollBtn.height;
}
}