How to keep volume slider fixed (when re-entering a scene) Flash - actionscript-3

So I have a scene which is an options menu; in the scene I have a volume slider; it works fine (I can slide it and the volume would go down/up), but when I leave the scene, it gives me an error (only happens if I play with the slider):
TypeError: Error #1009: Cannot access a property or method of a null object reference.
And it points at:
var myVolume:Number=V_Slider.V_Knob.x/mySliderLength;
and when I re-enter, the volume slider goes back to its original position. I'm very new to Flash and AS3, so any help I can get is very much appreciated.
Here is the code for the volume slider:
var dragging:Boolean=false;
var mySliderLength:uint=240;
var boundingBox:Rectangle=new Rectangle(0,0,mySliderLength,0);
V_Slider.V_Knob.addEventListener(MouseEvent.MOUSE_DOWN, dragKnob);
stage.addEventListener(MouseEvent.MOUSE_UP, releaseKnob);
V_Slider.V_Knob.buttonMode=true;
function dragKnob(myEvent:Event):void {
V_Slider.V_Knob.startDrag(false, boundingBox);
dragging=true;
V_Slider.V_Knob.addEventListener(Event.ENTER_FRAME, adjustVolume);
}
function releaseKnob(myEvent:Event):void {
if (dragging) {
V_Slider.V_Knob.stopDrag();
dragging=false;
}
}
function adjustVolume(myEvent:Event):void {
var myVolume:Number=V_Slider.V_Knob.x/mySliderLength;
var myTransform:SoundTransform=new SoundTransform(myVolume);
if (BGM_SC!=null) {
BGM_SC.soundTransform=myTransform;
}
}

It is a common problem man you can look for that. When u change you scene you need to remove your Events and when you come back to that scene u need to add them again. In this case you u can use this
function workEvents(action:String):void
{
if(action == "add")
{
V_Slider.V_Knob.addEventListener(MouseEvent.MOUSE_DOWN, dragKnob);
stage.addEventListener(MouseEvent.MOUSE_UP, releaseKnob);
}
else
{
V_Slider.V_Knob.removeEventListener(MouseEvent.MOUSE_DOWN, dragKnob);
stage.removeEventListener(MouseEvent.MOUSE_UP, releaseKnob);
}
}
when u use that gotoAndStop/gotoAndPlay(1,"Scene 2");
also use this: workEvents("rm");
this will remove your events and that problem should end :)
I hope it helps :))

Related

addChild and removeCurrent child

I have multiple buttons that load multiple movie clips. My problem is that when one loads it does not go away prevent the others to load when their button is clicked. Here is my code below. Do I need to add and "if" statement? Any help would be greatly appreciated. Thanks.
movieButton.addEventListener(MouseEvent.CLICK, gotoMovie);
webButton.addEventListener(MouseEvent.CLICK, gotoWeb);
mailButton.addEventListener(MouseEvent.CLICK, gotoMail);
function gotoMovie(event:Event):void {
var moviescene:MovieClip = new movie();
stage.addChild(moviescene);
}
function gotoWeb(event:Event):void {
var webscene:MovieClip = new web();
stage.addChild(webscene);
}
function gotoMail(event:Event):void {
var contactscene:MovieClip = new contact();
stage.addChild(contactscene);
}
I'm not entirely sure what you want to achieve, but I guess this should help.
var currentScene:MovieClip; // pointer to scenes
movieButton.addEventListener(MouseEvent.CLICK, gotoMovie);
webButton.addEventListener(MouseEvent.CLICK, gotoWeb);
mailButton.addEventListener(MouseEvent.CLICK, gotoMail);
// function that sets new scenes
function setScene(mc:MovieClip):void {
if(currentScene) stage.removeChild(currentScene);// if a scene is already loaded, remove it
currentScene = mc; // set currentScene to new scene
stage.addChild(currentScene); // add it to stage
}
function gotoMovie(event:MouseEvent):void {
setScene(new movie());
}
function gotoWeb(event:MouseEvent):void {
setScene(new web());
}
function gotoMail(event:MouseEvent):void {
setScene(new contact());
}

scrolling text field help as3

Currently got text off stage that I would like to come onto the stage and stop at a certain position (97, 233.10) on my stage. I'm a little confused on where to stop it and what code to use?
addEventListener(Event.ENTER_FRAME, mcInfo);
function mcInfo (e:Event):void {
//check position of logo
//if inside the stage move left to right
//if outside stage reposition
if (info.x<stage.stageWidth) {
info.x+=30;
stop();
} else {
//reset position
info.x=-450;
}
}
Cheers!
It also seems that Flash is now returning an output error when I scroll through the rest of my pages:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finaldesign_fla::MainTimeline/mcInfo()
In the if statement we check if the object has exceeded the target position and stop the loop if it has. Otherwise keep incrementing the object's position.
targetPosition = {x:97, y:233.10};
addEventListener(Event.ENTER_FRAME, mcInfo);
function mcInfo(e:Event) {
if (info.x >= targetPosition.x) {
info.x = targetPosition.x;
removeEventListener(Event.ENTER_FRAME, mcInfo);
} else {
info.x += 30;
}
}
make sure that info is on stage and try add:
if(info != null && stage != null)

Problems with an object appearing on a frame I don't want it to appear on AS3

I am a total noob at AS3, roughly 1 year experience so please be lenient with me :)
I currently am making an endless runner game and I'm making the obstacles spawn using this method
var therespawn:RespawnObject;
var thecone:trafficcone;
var started:Boolean = false;
var dx:Number = 10;
var dy:Number = 10;
stage.addEventListener(Event.ENTER_FRAME, startGame);
addEventListener(Event.ENTER_FRAME, collision);
addEventListener(Event.ENTER_FRAME, coneCollision);
function startGame(evt:Event):void {
if (started == false) {
spawnHazard();
}
}
function spawnHazard() {
started = true;
therespawn = new RespawnObject();
addChild(therespawn);
thecone = new trafficcone();
addChild(thecone);
therespawn.x = -50;
therespawn.y = 310;
thecone.x = 600;
thecone.y = 310;
}
function collision(evt:Event):void {
thecone.x -= 15;
if(thecone.hitTestObject(therespawn)) {
thecone.x = 600;
}
}
Now the only way to finish the game or end it is to get hit by an obstacle which ive shown down below:
function coneCollision(evt:Event):void {
if(MainChar.hitTestObject(thecone)) {
gotoAndStop("frameFive");
}
}
Everytime the highscore frame appears the cone is still spawning and despawning, why is that?
I haven't declared them as global?
Any help appreciated, thanks!
You can fix your problem by setting started to false:
function coneCollision(evt:Event):void {
if(MainChar.hitTestObject(thecone)) {
started = false;
gotoAndStop("frameFive");
}
}
The flash timeline really only has to with the way MovieClips are visually displayed as children of the stage. Removing an object from the timeline doesn't just suddenly nullify all the code associated with that object. In other words, your ENTER_FRAME method still runs in the background even if the object is no longer a child of the Stage, regardless of the frame number for the MovieClip. If you're serious about coding you might consider investigating in Classes and Object Oriented AS3. Classes are much nicer to work with than the Flash timeline.

Dragging movie clips in Action Script 3

Hi so recently I have been attempting to Drag a movie clip in AS3 but I'm having some trouble picking up with the hit tests anyone got any ideas? Just to clarify, the issue is that when the movieclips hit the drag test object, they're not executing the gotoframe() function.
initDrag() adds action listeners:
MOUSE_DOWN on the object
MOUSE_UP on the stage so it doesn’t matter if you are off the object
endDrag() removes the action listeners; call this (for each object) before you go to another frame
startADrag()create a rectangle within which the object can be dragged (in this case the stage)
call startDrag() on the object
stopADrag() call stopDrag() on the object from currentObject (but only if currentObject is not null).
var currentObject:MovieClip = null;
initDrag(block1);
initDrag(block2);
initDrag(block3);
initDrag(block4);
function initDrag(obj:MovieClip )
{
obj.addEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.addEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function endDrag(obj:MovieClip )
{
obj.removeEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.removeEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function startADrag(e:MouseEvent):void
{
currentObject = (MovieClip)(e.target);
var rect:Rectangle = new Rectangle(0,0,stage.stageWidth - currentObject.width,stage.stageHeight - currentObject.height + 100);
currentObject.startDrag(false,rect);
}
function stopADrag(e:MouseEvent):void
{
if (currentObject != null)
{
currentObject.stopDrag();
}
}
if(block1.hitTestObject(dragtest)){
gotoAndStop("lose");
}
if(block2.hitTestObject(dragtest)){
gotoAndStop(27);
}
if(block3.hitTestObject( dragtest)){
gotoAndStop("lose");
}
if(block4.hitTestObject( dragtest)){
gotoAndStop("lose");
}
thanks for any advice or answers.
The following code should work as expected. The problem is, as i already stated in my comment, that your calls to hitTestObject(obj) only get executed once, at the very beginning of your application. What you need to do though is to check it constantly.
Think about it, if your calls to hitTestObject-calls only get executed once at the beginning, when you didn't even have a chance to drag one of your objects, it will always return false, right? Because your objects are still in their initial position (outside of the dragtest objecti must assume).
With an event listener for Event.ENTER_FRAME you check it once per frame instead. So even if all the results for hitTestObject are false, it will check them all again on the next frame (if you are currently dragging, controlled through a simple boolean called dragging).
var currentObject:MovieClip = null;
var dragging:Boolean = false;
initDrag(block1);
initDrag(block2);
initDrag(block3);
initDrag(block4);
addEventListener(Event.ENTER_FRAME, checkForHit);
function checkForHit(e:Event):void{
if(dragging){
if(block1.hitTestObject(dragtest)){
gotoAndStop("lose");
}
if(block2.hitTestObject(dragtest)){
gotoAndStop(27);
}
if(block3.hitTestObject( dragtest)){
gotoAndStop("lose");
}
if(block4.hitTestObject( dragtest)){
gotoAndStop("lose");
}
}
}
function initDrag(obj:MovieClip )
{
obj.addEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.addEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function endDrag(obj:MovieClip )
{
obj.removeEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.removeEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function startADrag(e:MouseEvent):void
{
currentObject = (MovieClip)(e.target);
var rect:Rectangle = new Rectangle(0,0,stage.stageWidth - currentObject.width,stage.stageHeight - currentObject.height + 100);
currentObject.startDrag(false,rect);
dragging = true;
}
function stopADrag(e:MouseEvent):void
{
if (currentObject != null)
{
currentObject.stopDrag();
dragging = false;
}
}

Action Script 3: Problems with gotoAndStop() after a Movie Clip

Upon the click of a button, an animation starts. Then the program directs you to a certain frame when the animation is done.
Is this possible?
So this is what I've got so far: a Movie Clip movQuizIntro and a Button btnBond in Frame 1.
stop()
movQuizIntro.stop()
btnBond.addEventListener(MouseEvent.CLICK, BondQuiz)
btnReg.addEventListener(MouseEvent.CLICK, Registrering)
function BondQuiz (evt:MouseEvent)
{
if (currentFrame == 1)
{
movQuizIntro.alpha = 1
movQuizIntro.play()
}
}
What is the code and proper syntax you need to write in order to go to frame 2 after the animation is done?
`
stop();
movQuizIntro.stop();
int frameCounter=0;
btnBond.addEventListener(MouseEvent.CLICK, BondQuiz);
btnReg.addEventListener(MouseEvent.CLICK, Registrering);
function BondQuiz (evt:MouseEvent)
{
if (currentFrame == 1)
{
movQuizIntro.alpha = 1
movQuizIntro.play()
movQuizIntro.addEventListener(EventType.ENTER_FRAME, onEnterFrame);
}
}
// event handler function, runs every enter frame
private function onEnterFrame(event:Event):Void
{
frameCounter++;
if(frameCounter > movQuizIntro.totalFrames)
{
//Place code here because you know the MovieClip finished playings
//Go to desired frame
}
}
`
I wrote this code outside of an editor nor did I get to compile, so the gist is there and may have some minor errors.
NOTE:This is just a quick way of doing this. If you want something more reusable/cleaner then you would want to consider subclassing or alternate Object Oriented tricks.
In button event handler:
function onClick(e:MouseEvent):void{
ANIMATION_MC.addEventListener(Event.EXIT_FRAME, onFromeExit);
}
function onFrameExit(e:Event):void {
if (ANIMATION_MC.currentFrame == SOME_FRAME) {
ANIMATION_MC.removeEventListener(Event.EXIT_FRAME, onFromeExit);
TARGET.gotoAndPlay(NEW_FRAME);
}
}
And you can just use addFrameScript on ANIMATION_MC too.