Volume slider - volume doesn't change until mouse over - actionscript

I've created a small music player with a sliding volume control. I'm having trouble with the volume. Though it does control volume properly, if I set the initial volume to less than 100%, the volume always starts at 100% until I move my mouse over the player. At that point, the volume changes to whatever the initial volume is set to.
Is this a flash bug, or am I missing something?
Here is the affected code (code for other buttons/functions omitted for brevity):
var song_initvolume:Number = 100;
slider_1._x = groove_1._x + song_initvolume;
playSong(0,song_play);
slider_1.onPress = function() {
this.startDrag(true, groove_1._x, groove_1._y, groove_1._x + 96, groove_1._y);
}
slider_1.onRelease = function() {
this.stopDrag();
}
slider_1.onMouseMove = function(){
newPoint = new Object();
newPoint.x = this._x;
newPoint.y = this._y;
groove_1.globalToLocal(newPoint);
mySound_sound.setVolume(-1 * newPoint.x);
}
function playSong(songNum,songPlay,reset_Pos){
if(reset_Pos){
mySound_sound = new Sound();
}
var myTitle = mySongs_array[songNum].TITLE;
trace("Playing TITLE= " + myTitle);
var myArtist = mySongs_array[songNum].ARTIST;
trace("Playing ARTIST= " + myArtist);
var myURL = mySongs_array[songNum].URL;
trace("Playing URL= " + myURL);
title_txt.text = mySongs_array[songNum].TITLE;
artist_txt.text = mySongs_array[songNum].ARTIST;
mySound_sound.loadSound(myURL,songPlay);
// start oncomplete
mySound_sound.onSoundComplete = function() {
song_pos = 0;
reset_Pos = true;
if(song_continuous){
song_play = true;
current_song++;
if (current_song>=my_total){
current_song=0;
if(song_loop){
song_play = true;
} else {
song_play = false;
}
}
} else {
song_play = false;
}
playSong(current_song,song_play,reset_Pos);
}
// end oncomplete
}
I'd like to be able to set the volume at say 50%, but the above mentioned behavior happens each time.
Any ideas are greatly appreciated.

You need to call mySound_sound.setVolume() with your initial value. Right now you only do in in the onMouseMove handler.

Just fixed with this code:
mySound_sound.onLoad = function(){
mySound_sound.setVolume(-1 * song_volume);
}
Works great now!

Related

Need code to make animation to stop

Trying to stop this animation on frame 109 in as2. Any help in what I need to ad to get this to stop. It is a confetti animation.
cnfNumber = 85;
var cnfs:Array = new Array("cnf", "cnf2", "cnf3","cnf4","cnf5")
for (i=0; i<cnfNumber; i++) {
newCnf = _root[cnfs[random(cnfs.length)]].duplicateMovieClip("snow"+i, i);
newCnf._x = Math.random()*Stage.width;
newCnf._y = -Math.random()*300;
newCnf.maxCnfSpeed = Math.random()*4;
newCnf.wind = Math.random()*6;
newCnf._xscale = newCnf._yscale = newCnf.alpha = Math.random()*65+35
newCnf.onEnterFrame = function() {
this._rotation -= this.maxCnfSpeed+1;
if(this._y>Stage.height || this._x>Stage.width || this._x<0){
this._y = -Math.random()*300;
this._x = Math.random()*Stage.width;
} else {
this._y += this.maxCnfSpeed+2;
this._x += this.wind-4;
}
this._alpha = this.alpha;
};
}
Why not just do this:
newCnf._xscale = newCnf._yscale = newCnf.alpha = Math.random()*65+35
newCnf.frameCount = 0;
newCnf.onEnterFrame = function() {
if(this.frameCount<109) {
this._rotation -= this.maxCnfSpeed+1;
if(this._y>Stage.height || this._x>Stage.width || this._x<0){
this._y = -Math.random()*300;
this._x = Math.random()*Stage.width;
} else {
this._y += this.maxCnfSpeed+2;
this._x += this.wind-4;
}
this._alpha = this.alpha;
this.frameCount++;
} else {
this.onEnterFrame = null;
}
};
I haven't worked in Actionscript in a while, so the this.onEnterFrame = null may not be quite correct, but it's not strictly necessary; it just keeps the confetti from checking the frame number and bailing out every frame once it's done animating.
If you want the confetti to disappear rather than freezing in place, unload it instead of clearing its onEnterFrame. I don't remember the function that does that off the top of my head, but the documentation for duplicateMovieClip should have a link to it.
(I won't ask why you're still using AS2.)

Titanium ImageView animation width and height

I try to create an ImageView in Titanium like this:
var animationView = Titanium.UI.createImageView
(
{
images:animationImages,
duration:50,
repeatCount:0,
width: '90dp',
height: '270dp'
}
);
On android it gets its size as expected, but on IOS, it simply doesn't gets scaled. Is there something, i'm doing wrong? Or should i do it frame by frame by creating the ImageViews manually then changing them with setInterval?
This is actually not a consistent solution, it should be a comment, but since I don't have enough rep I have to write it as an answer.
For starters I would try to give it a top and left properties.
Secondly, are those images retrieved from a remote URL? Remote URLs are only supported in Android. If that is the case you could do a workaround as you said in the question.
Finally, the 'dp' only works for android, so it won't scale at all in iOS, it will simply erase the 'dp' and use the number as "points", on non-retina screens it will be the same number of pixels and on a retina display it will be the double.
I finally decided to create my own animation class, which look like this:
function Animation(data)
{
var width = data.hasOwnProperty("width") ? data.width : Ti.UI.SIZE;
var height = data.hasOwnProperty("height") ? data.height: Ti.UI.SIZE;
var duration = data.hasOwnProperty("duration") ? data.duration : 50;
var imageFiles = data.hasOwnProperty("images") ? data.images : [];
var images = [];
var container = Ti.UI.createView
(
{
width:width,
height: height
}
);
for(var i=0; i<imageFiles.length; i++)
{
var image = Ti.UI.createImageView
(
{
image:imageFiles[i],
width:width,
height:height
}
);
if(i!=0)
image.setVisible(false);
container.add(image);
images.push(image);
}
container.activeImage = 0;
container.intervalId = null;
container.setActiveImage = function(index)
{
if(container.intervalId == null)
container.activeImage = index;
}
container.start = function()
{
var callback = function()
{
for(var i=0; i<images.length; i++)
{
if(i == container.activeImage)
images[i].setVisible(true);
else
images[i].setVisible(false);
}
container.activeImage = (container.activeImage + 1) % images.length;
}
container.intervalId = setInterval ( callback, duration );
}
container.stop = function()
{
clearInterval(container.intervalId);
container.intervalId = null;
}
return container;
}
module.exports = Animation;
And you can use it like this:
var Animation = require('...path to your animation file');
var myAnimation = new Animation
(
{
width:'100dp',
height:'100dp',
duration:50, //duration while one frame is showing
images:['one.png', 'two.png'...], //full paths
}
);
//start:
myAnimation.start();
//stop
myAnimation.stop();

Flash NetStream.seek method works in local tests but not on server

I have a 20 minute FLV which streams just fine on server. Client would like to preserve user's locations between sessions so time() is saved to mySQL and passed back in as a FlashVar and is (if set) fed to seek() and to a text field for testing. Thing is the seek() works fine locally but on the server I always get a NetStream.Seek.InvalidTime error no matter what the seek() is set to. Docs are here; it's a dead simple function.
// EDIT Just added keyframes to FLV using http://www.buraks.com/flvmdi/ but this did not resolve issue
src = "videos/LivingProof.flv";
nc = new NetConnection();
nc.connect(null);
nets = new NetStream(nc);
mc_flv.attachVideo(nets);
//Attach your netstream audio to a movielcip:
snd.attachAudio(nets);
// create a sound object
my_snd = new Sound(snd);
// to adjust the volume
my_snd.setVolume(50);
nets.play(src);
if (starttime) {
var dest:Number = Math.floor(starttime);
nets.seek(dest);
this.test.text = 'target time = ' + dest;
}
nets.onStatus = function(infoObject:Object) {
if( infoObject.level == "status" && infoObject.code == "NetStream.Play.Stop" ) {
getURL("javascript:setTime('9999999999');", "_self");
nets.seek(0);
nets.pause();
mc_play.gotoAndStop(1);
trace('onStatus listener fired');
} else if (infoObject.code == "NetStream.Seek.InvalidTime") {
_root.test.text = "NetStream.Seek.InvalidTime";
nets.seek(infoObject.details);
}
_root.status.text = infoObject.code;
};
Anyone ever seen this before?
Try adding an if statement to your onStatus handler to check for the NetStream.Play.Start code and move the seek logic to that:
src = "videos/LivingProof.flv";
nc = new NetConnection();
nc.connect(null);
nets = new NetStream(nc);
mc_flv.attachVideo(nets);
//Attach your netstream audio to a movielcip:
snd.attachAudio(nets);
// create a sound object
my_snd = new Sound(snd);
// to adjust the volume
my_snd.setVolume(50);
nets.play(src);
nets.onStatus = function(infoObject:Object) {
if( infoObject.level == "status" && infoObject.code == "NetStream.Play.Stop" ) {
getURL("javascript:setTime('9999999999');", "_self");
nets.seek(0);
nets.pause();
mc_play.gotoAndStop(1);
trace('onStatus listener fired');
} else if (infoObject.code == "NetStream.Play.Start) {
if (starttime) {
var dest:Number = Math.floor(starttime);
nets.seek(dest);
this.test.text = 'target time = ' + dest;
}
} else if (infoObject.code == "NetStream.Seek.InvalidTime") {
_root.test.text = "NetStream.Seek.InvalidTime";
nets.seek(infoObject.details);
}
_root.status.text = infoObject.code;
};

Actionscript 2: Tween running extremely slow

I am using the following code to tween an movieclip once _global.choiceMade equals 1...
onClipEvent (load) {
import mx.transitions.Tween;
import mx.transitions.easing.*;
}
onClipEvent (enterFrame) {
if (_global.choiceMade == 1) {
var myTweenX:Tween = new Tween(this, "_x", mx.transitions.easing.Back.easeOut, this._x, -349, 0.5, True);
}
}
This is contained within each frame of the main timeline. The first time it runs fine but on the next frame it runs incredibly slow (takes about 12 seconds not 0.5 and is very choppy), if I then return to the first frame and run it again now this time it is extremely slow.
I can't work out why its doing this my CPU stays around 6-15% while its running so it can't be too demanding.
Updated to show rest of my code:
On main timeline have a frame each containing a movieclip. On the timeline of each of these movieclips contains:
//tint an object with a color just like Effect panel
//r, g, b between 0 and 255; amount between 0 and 100
Color.prototype.setTint = function(r, g, b, amount) {
var percent = 100-amount;
var trans = new Object();
trans.ra = trans.ga=trans.ba=percent;
var ratio = amount/100;
trans.rb = r*ratio;
trans.gb = g*ratio;
trans.bb = b*ratio;
this.setTransform(trans);
};//Robert Penner June 2001 - http://www.robertpenner.com
MovieClip.prototype.scaleXY = function(to){
this.onEnterFrame = function(){
this._alpha = to-(to-this._alpha)/1.2;
if(this._alpha > to-1 && this._alpha < to+1){
this._alpha = to;
delete this.onEnterFrame
}
}
}
scoreUpdated = 0;
Answer = 1;
_global.choiceMade = 0;
Buttons = new Array(this.buttonHolder.True, this.buttonHolder.False);
Answers = new Array(this.Correct, this.Wrong);
for (i=0; i<Answers.length; i++) {
Answers[i]._alpha = 0;
}
for (b=0; b<Buttons.length; b++) {
Buttons[b].thisValue = b;
}
In this movieclip there are two movieclip buttons (True and False) containing this code:
onClipEvent (enterFrame) {
this.onRollOver = function() {
this.gotoAndStop("over");
};
this.onRollOut = function() {
this.gotoAndStop("up");
};
this.onPress = function() {
this.gotoAndStop("down");
};
this.onReleaseOutside = function() {
this.gotoAndStop("up");
};
this.onRelease = function() {
this.gotoAndStop("down");
whichChoice = this;
_global.choiceMade = 1;
counter = 0;
};
if (_global.choiceMade == 1) {
this.enabled = false;
this._parent.scoreNow = _global.score;
this._parent.scoreOutOf = (this._parent._parent._currentframe)- 1 + ( _global.choiceMade);
if (thisValue == this._parent._parent.Answer && whichChoice == this) {
myColor = new Color(this);
myColor.setTint(0,204,0,13);
this._parent._parent.Answers[0]._alpha = 100;
this._parent._parent.Answers[0].scaleXY(100);
this.tick.swapDepths(1000);
if (counter == 0) {
_global.score++;
counter++;
}
}
else if (thisValue == this._parent._parent.Answer) {
myColor = new Color(this);
myColor.setTint(0,204,0,13);
this.tick.swapDepths(1000);
}
else if (whichChoice == this) {
this._parent._parent.Answers[1]._alpha = 100;
this._parent._parent.Answers[1].scaleXY(100);
myColor = new Color(this);
myColor.setTint(255,0,0,13);
this.cross.swapDepths(1000);
}
else {
myColor = new Color(this);
myColor.setTint(255,0,0,13);
myColor.setTint(255,0,0,13);
this.cross.swapDepths(1000);
}
}
}
The script at the top is on a movieclip these buttons are contained in called buttonHolder which does what it says, and tweens the buttons across the screen to reveal a next button once an answer is chosen.
From what I can see, as long as you have choiceMade == 1 you create a new effect! which is not ok. because in 1 sec at 15 fps you will have 15 tweens running :(
try yo set choiceMade = 0 or somehting else than 1
onClipEvent (enterFrame)
{
if (_global.choiceMade == 1)
{
_global.choiceMade = -1;
var myTweenX:Tween = new Tween(this, "_x", mx.transitions.easing.Back.easeOut, this._x, -349, 0.5, True);
}
}
Without seeing the rest of your code it's hard to see exactly what's going on. But it looks like you never change choiceMade and it continuously recreates the tween.
onClipEvent (enterFrame) {
if (_global.choiceMade == 1) {
var myTweenX:Tween = new Tween(this, "_x", mx.transitions.easing.Back.easeOut, this._x, -349, 0.5, True);
_global.choiceMade = 0;
}
}

Flash: How to preload upcoming SWF while current one plays

I have a Flash slideshow that plays SWFs listed in an XML file. I would like to have the upcoming SWF load while the current one displays. I've tried all sorts of combinations of LoadMovie and LoadMovieNum, including creating an empty movie clip, but there's something I'm just not getting.
Right now, after making the first round through all the files, it transitions smoothly from slide to slide, but I'd like for it to preload so that it transitions without the "Loading..." screen the first time around.
It can be viewed here: slideshow
Where should I put the LoadMovie line to load the next file (image[p+1]), and how should it look?
function loadXML(loaded) {
if (loaded) {
xmlNode = this.firstChild;
image = [];
description = [];
total = xmlNode.childNodes.length;
for (i=0; i<total; i++) {
image[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
description[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
}
firstImage();
} else {
content = "file not loaded!";
}
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("xmlfile.xml");
/////////////////////////////////////
back_btn.onRelease = function ()
{
backImage();
};
next_btn.onRelease = function ()
{
nextImage();
};
p = 0;
function nextImage() {
if (p<(total-1)) {
p++;
trace(this);
_root.mc_loadfile.loadMovie (image[p]);
_root.movie_name.text = image[p];
next_btn._visible = true;
back_btn._visible = true;
if (getBytesLoaded() == getBytesTotal())
slideshow();
}
else if (p == (total-1)) {
p = 0;
firstImage();
}
}
function backImage() {
clearInterval(myInterval);
if (p>0) {
--p;
_root.mc_loadfile.loadMovie (image[p]);
_root.movie_name.text = image[p];
next_btn._visible = true;
if (p != 0) {
back_btn._visible = true;
}
else {
back_btn._visible = false;
}
slideshow();
}
}
I'd appreciate any help.
You can do it with the MovieClipLoader class. A brief tutorial can be found at actionscript.org, or you can check the docs on it.

Resources