When playing back sounds the more sounds that play at once the higher pitch and louder they seem to get - actionscript-3

basically what the title says when i play back sounds using the basic
mySound= new Sound();
c = new SoundChannel();
c = mySound.play();
this is in a class that is being set to a mp3 for testing. I did have it generating sound but set it to a mp3 for testing the pitch problem. The more times it is called while the same sound is still playing the higher pitch it seems to become almost becoming louder as well. I thought that was the point of different channels? Is there same way to fix this with soundmixer or something? Thank you
Please don't downvote just because you don't understand what i am asking I see this happening way too often lately.

mySound.play() is returning a new SoundChanel.
c = new SoundChannel();
c = mySound.play();
What you're doing here is assigning a new SoundChanel to c, juste after that, you assigne it an other new SoundChanel.
And assigning a new SoundChanel to c do not make the previous one disappear. It still exist (an thus playing), but c is not refering to it anymore.
Try this:
if(c!=null) // if c has previously be assigned a soundchanel (that might be playing), stop it
c.stop();
c = mySound.play(); // assign new SoundChanel to c and play it

Related

Need Volume adjust on as3 tips

This is my code for sound in my program. I'm not sure if the mySound and mySound2 are necessary. They are different music tracks but will not be playing at the same time. There are some errors like the incorrect sequence ones, but I can fix those. I'm worried more about the volume because it tends to be a little loud without the volumeAdjust. The thing that I'm worried about is that no matter what I set the volumeAdjust.volume to it does not change anything about the volume. I've tried stuff like
volumeAdjust -= .5
not in the function but as a global variable/object, but that doesn't work either.
Here is the essential code of it, but bear in mind the channels are already declared variables globally.
var volumeAdjust:SoundTransform = new SoundTransform();
var mySound:Sound = new Sound();
var mySound2:Sound = new Sound();
volumeAdjust.volume = 0.5;
channel.soundTransform = volumeAdjust;
channel2.soundTransform = volumeAdjust;
So I thought I would just add the solution I came up with which involves Organis's tip. I didn't think that it would work because the sounds are short, but putting the volume adjust right after the .play(); line does work I'm thinking almost immediately so. I was unsure how well the program would take the quick sound and adjust it quickly enough but it did work the way I wanted it to.

As3 - I Simply cannot remove loader child no matter what

I have loaded an external SWF to my timeline using a common loader method. The SWF is Jwplayer and it is polling a live rtmp stream. Here is the snippet I used to load it successfully.
var c4:Loader = new Loader();
var c4req:URLRequest = new URLRequest("player.swf");
var c4flashvars:URLVariables = new URLVariables();
c4flashvars.streamer = "rtmp://myserver";
c4flashvars.file = "live"
c4req.data = c4flashvars;
c4.load(c4req);
addChild(c4)
Now, the player loads fine. It begins to poll a live stream, too. However, no matter what I try I cannot remove this child from the stage. I have tried the following:
removeChild(c4);
c4.unloadAndStop();
For testing, I am calling these from a timer/event listener/function combo. I traced it to make sure it was being fired, and it is. However the player just sits there on the stage and refuses to leave. Output isn't throwing any errors. I half suspect that because it's polling a live stream that the loader never sees the external SWF as fully loaded. But that's just a hunch.
The overall scope of this little project is to load the player, check for an event, and re-load the player (unload, and reload). If anyone could help me understand this, get past this, or is feeling generous enough to show me the light; I'd appreciate it greatly. I've put a solid day into messing around with this and searching. I'm just not grasping something. Thanks!

Split screen in Actionscript 3

I'm trying to make a pure AS3 game, and need a way to split the screen so that two players can have individual "cameras" that follow the around the game world. The problem is that a sprite can't have multiple parents. I'm trying to hack my way around this problem by having classes that duplicate sprites and manage all of their updates, but I'm not getting very far and my code is getting very, very ugly.
Does anyone know a good workaround or method for doing this? I can't seem to find much on-line on the subject.
I think you should use BitmapData copyPixels method
.copyPixels(point_0, rectangle_0)---> FirstPlayerScreen
World.Bitmap -
.copyPixels(point_0, rectangle_0)---> SecondPlayerScreen
Thanks for everyone's suggestions. What I've ended up with is a World class that I can add regular Sprite objects to as children. The World object manages and updates copies of those sprites, and world.getCamera() can be called as many times as necessary to display custom copies of the game world.
The key part is making copies of the sprites, this is the function I wrote to do that:
public function bitmapCopy(original:DisplayObject):Sprite
{
var returnSprite:Sprite = new Sprite();
if (original.width == 0 || original.height == 0) return returnSprite
var x = original.x; var y = original.y
var rotation = original.rotation
original.x = 0; original.y = 0; original.rotation = 0
var bounds:Rectangle = original.getBounds(original.parent)
var m:Matrix = new Matrix()
m.tx = -bounds.x
m.ty = -bounds.y
var bitmapData:BitmapData = new BitmapData( bounds.width, bounds.height, true,
0xFF0000 );
bitmapData.draw(original, m);
var bitmap:Bitmap = new Bitmap( bitmapData );
bitmap.x = bounds.x
bitmap.y = bounds.y
returnSprite.addChild(bitmap);
returnSprite.x = original.x = position.x
returnSprite.y = original.y = position.y
returnSprite.rotation = original.rotation = rotation
return returnSprite;
}
The returned Sprite can be added to the stage and will act in exactly the same way as the original (except for being static, of course). Hopefully this should help anyone else who comes across this problem.
Your theory is on the right track - you're probably just stumbling over your implementation. Organization and keeping things object-oriented will be your best friend in this scenario.
It's hard to give you a good example based off of a limited description of your game, but in general, I'd have a Screen class that can be instantiated multiple times, and I'd keep track of each instantiation (that gives us our multiple "players"). Each Screen has a stage, and you're building your game world on that stage.
The key here is data organization and good communication. Remember, there's only one "world", and you're just showing multiple instances of it. So you'll want a central Model to store data about every object in your singular game world. That Model will drive the rendering of that world to your multiple screens. If a player changes an object on their screen (let's say they move it), then you'll update the Model with that object's new location. Then, you'll broadcast those new coordinates to each Screen instance, so that all of your screens will update.
How you "broadcast" this can vary (and depends largely on the real-time nature of your game). If your game is very real-time and you're running a game loop, then you may just want to pass the objects' data along in every loop, and they'd update that way. If your game isn't as dependent of being real-time, then you can set up event listeners or a custom notification system that'll alert all of the instances of an object to update itself.
I know this is very high-level, but it's hard to give an in-depth answer without more info about your game. Hopefully this helps point you in the right direction - what you're attempting is definitely not simple, so don't get discouraged!
If you develop your game using MVC architecture principles then it should be trivial to draw your game twice, each player having an instance of the game's "view" but positioned according to a different character. If you give a layer mask to each instance then you could put the two instances side by side and so create a split screen effect.
Happy coding!
I'm working on a split screen myself just now:
masking to draw 1 world in 2 branches of your DisplayObject tree (separated cleanly), copying pixels for a split screen is a bad idea
data object to describe the world (not Sprite or DisplayObject)
Works very well so far. I've got 1 level, 2 players who move independently from each other and 2 screens that follow one player at a time. I see the other player in one player's screen and the one player in the other player's screen.
Here is how I did the data object part:
Define a central data object which describes the world and all it's world objects.
Write: make some sprites being able to manipulate 1 object of the world.
Read: update sprites by checking the properties of the world objects. 2 Screens -> 2 sprites for every world object. Check them every frame or try events.

unload dynamically loaded swf after displaying it AS3

I am doing a presentation on which I need to use a lot of video clips. I load all these videos dynamically using Loader. I use a code snippet like this:
var req:URLRequest = new URLRequest("video.swf");
var a:Loader = new Loader();
a.load(req);
stage.addChild(a);
a
Now the issue is: When I get to say the 7th one, it starts lagging. I do not know why, but I think it is because everything is loaded to memory. Is there a way I can erase a video from memory after displaying it? Or is there another solution to this?
If you just load a new video each time and add it to the stage, you will have a rather big hierarchy of movies sitting on top of each other; all kept on stage and in memory.
If you keep a reference to the previously loaded movie, you can just remove it from the stage when the 'next' movie is loaded. Haven't tested this properly, but just to give you an idea of what the code might look like:
var oldLoader:Loader = null;
var a:Loader = new Loader();
a.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
a.load(<your url>);
function loader_complete(e:Event):void {
var newLoader:Loader = e.currentTarget.loader as Loader;
stage.addChild(newLoader);
if(oldLoader != null) {
stage.removeChild(oldLoader);
oldLoader = null;
}
oldLoader = newLoader;
}
I think I found the solution. If you use removeChild, it only stops displaying, but the whole thing is still in memory (cache). So the more you load the videos, the more you fill up the cache. What we need is something that completely clears the cache before loading another video.
I used unloadAndStop() and it seem to have completely done the trick, unless someone has something against it.
Thanks again

soundTransform is muting the sound but sound still plays

I am using the following code, in the last line when I play the sound I can still hear it even when I have transformed it to 0.
var tempTransform:SoundTransform = new SoundTransform(0,0.5);
clickSoundChannel = clickSound.play();
clickSoundChannel.soundTransform = tempTransform;
clickSound.play();
You're calling clickSound.play() twice, and you're only muting the first one.