Resize the movie clip when change the frame - actionscript-3

Please help, i have the following code ( Two frames, ones logined it goes to frame 2 and stop. ).
What i am after is to change the whole movie clip size to very small ones it's in frame2. please any advice is much appreciated.
stop();
stage.addEventListener(MouseEvent.RIGHT_CLICK, function(e:Event){});
login_btn.buttonMode = true; // show hand cursor
//adding event listener
login_btn.addEventListener(MouseEvent.CLICK, loginUser);
function loginUser(e:MouseEvent):void{
//if username & password are correct allow entry
if(IDTxt.text == "wixlab"){
gotoAndStop(2);
}
if(IDTxt.text == "hello"){
gotoAndStop(2);
}
if(IDTxt.text == "hello"){
gotoAndStop(2);
}else {
//otherwise show an error message
var alert:TextFormat = new TextFormat();
alert.color = 0xff0000; //red font
messageTxt.text = "Incorrect ID, Please try again.";
messageTxt.setTextFormat(alert);
}
}

If you want to get rid of the button, there are many ways to do it:
Get rid of the button permanently
removeChild(login_btn);
If you actually just want to shrink the button so you can't see it
login_btn.scaleX = 0;
If you want the button to still exist but not see it:
login_btn.x = -1000;

Related

Actionscript to make movie clip invisible before another is opened

I have a basic flash document that is showing football fixtures spread over several weeks. I have buttons for each week e.g. 'Gameweek 1', 'Gameweek 2' etc.
When the button is pressed, a movie clip is displayed below the buttons which shows the fixtures. The fixtures movie clip is always there but the button changes it to visible.
My problem is...if I have gameweek 1 fixtures showing and then i click on the 'Gameweek 2' button, both sets of fixtures are displayed because the gameweek 1 fixtures are still visible.
When I press a button to display new fixtures, I would like the previously visible movie clip to now be invisible so that just the new fixtures are visible.
Here is my actionscript:
stop();
btn_game1.addEventListener(MouseEvent.CLICK,openGame1);
function openGame1(evt:MouseEvent) {
if (game1.visible){
game1.visible = false;
}else {
game1.visible = true;
}
}
btn_game2.addEventListener(MouseEvent.CLICK,openGame2);
function openGame2(evt:MouseEvent) {
if (game2.visible){
game2.visible = false;
}else {
game2.visible = true;
}
}
I would like to propose a re-factored approach that cuts down on the amount of code and number of event handlers.
You only really need one button handler for all of your buttons. You'll also want to keep track of all of the clips that you're showing/hiding. They can be stored in an array.
You can "connect" a button to a clip by giving them similar names (btn_game1 and game1).
The following code assumes that your buttons are all named btn_gameN and your clips are named gameN (where N is a number):
var clips:Array = [game1, game2, game3, game4];
btn_game1.addEventListener(MouseEvent.CLICK, onGameButtonClicked);
btn_game2.addEventListener(MouseEvent.CLICK, onGameButtonClicked);
btn_game3.addEventListener(MouseEvent.CLICK, onGameButtonClicked);
btn_game4.addEventListener(MouseEvent.CLICK, onGameButtonClicked);
function onGameButtonClicked(e:MouseEvent):void
{
// figure out which button was just clicked by looking at its name
var clipNum:String = e.currentTarget.name.substr("btn_game".length);
// loop through all of your clips ...
for each(var clip:MovieClip in clips)
{
if(clip.name.substr("game".length) == clipNum)
{
// if the name matches, toggle the visibility
clip.visible = !clip.visible;
}
else
{
// if the name doesn't match, set visibility to false
clip.visible = false;
}
}
}
When you show game1 you must hide game2. Likewise for game2.
function openGame1(evt:MouseEvent) {
if (game1.visible) {
game1.visible = false;
}else {
game1.visible = true;
game2.visible = false;
}
}

Going from one scene to next and gettingType1009 Null object reference errors

Okay so in my main menu for my game, I have a glow effect so that when you hover over the invisible button 'startdesu', the script will make the whole menu glow, fading in and out. This code (from the event listener onwards) has been repeated for each button on the menu, with appropriate changes to the button instance name and the function names. The only problem is, when I click 'startdesu' and it takes me to the next scene, I start getting repetitive errors, "TypeError: Error #1009: Cannot access a property or method of a null object reference.
at bjvb_fla::MainTimeline/increaseGlow()". I've tried removing the glow event listeners when the buttons are clicked and everything but no luck. Please help! ;0;
Here is the essential code for brevity's sake, but I can post the whole thing if it helps.
(also, i get the same Null error for something else when i go from the game back to the start menu.
import flash.filters.*;
import flash.events.Event;
stop();
wow.alpha=0.5;
var menuGlow:GlowFilter = new GlowFilter();
menuGlow.color = 0xffffff;
menuGlow.blurX = 20;
menuGlow.blurY = 20;
menuGlow.alpha = 0;
menu.filters = [menuGlow];
var glow:Boolean = false;
startdesu.addEventListener(MouseEvent.MOUSE_OVER, addGlow);
startdesu.addEventListener(MouseEvent.MOUSE_OUT, removeGlow);
startdesu.addEventListener(Event.ENTER_FRAME, increaseGlow);
function addGlow(e:MouseEvent):void
{
glow = true;
}
function removeGlow(e:MouseEvent):void
{
glow = false;
}
function increaseGlow(e:Event):void
{
if(glow == true)
{
menuGlow.alpha = menuGlow.alpha + 0.02;
}
else if(glow == false)
{
menuGlow.alpha = menuGlow.alpha - 0.02;
}
menu.filters = [menuGlow];
}
This is the line that is likely causing the error:
menu.filters = [menuGlow];
There is probably no object with the instance name 'menu' in your second scene. You could fix the error by just adding a check if the object exists, like so:
function increaseGlow(e:Event):void
{
//if there's no menu object, return
if(!menu) return;
if(glow == true) {
menuGlow.alpha = menuGlow.alpha + 0.02;
} else {
menuGlow.alpha = menuGlow.alpha - 0.02;
}
menu.filters = [menuGlow];
}
But the correct solution would be to remove the event listeners. Not sure why it didn't work for you, but you should be able to just add this in the click event handler before you switch your scene.
startdesu.removeEventListener(Event.ENTER_FRAME, increaseGlow);
What errors do you get when you try to remove the event listener?

Actionscript 3 drag and drop on multiple specific targes and change alpa for the dropped objects as well as stack targets

I have been trying to achieve three things in the project without success. I am new at this and have relied on tutorials to get this far. Here we go!!
a. I want to be able to drop label_3 and label_4 on either or targetlabel_3 and targetlabel_4 but not effect the other labels and targets.
b. I want to be able to drop label_2 on top of label_1 once it has been dropped. I am finding that when label_1 has been dropped, it hides the targetlabel_2 and label_2 can't find it's target.
c. I want to change the Alpa of each of labels _1, _2, _3, _4 and _5 to zero when they are dropped on their targets and change the Apha for labels _11, _21, _31, _41 and _51 to 100. (I have changed the Apha to 25 on these for the sake of making it easier for someone to see what I am trying to do).
I have been mucking around for days on this and have hit a brick wall.
Can anyone help please?
import flash.display.DisplayObject;
import flash.geom.Rectangle;
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
var startX:Number;
var startY:Number;
var counter = 0;
var attempts = 0;
var rect:Rectangle;
rect=new Rectangle(100,100,700,500);
correct_txt.text=counter;
attempts_txt.text=attempts;
label_1.addEventListener(MouseEvent.MOUSE_DOWN,Drag);
label_1.addEventListener(MouseEvent.MOUSE_UP,Drop);
label_2.addEventListener(MouseEvent.MOUSE_DOWN,Drag);
label_2.addEventListener(MouseEvent.MOUSE_UP,Drop);
label_3.addEventListener(MouseEvent.MOUSE_DOWN,Drag);
label_3.addEventListener(MouseEvent.MOUSE_UP,Drop);
label_4.addEventListener(MouseEvent.MOUSE_DOWN,Drag);
label_4.addEventListener(MouseEvent.MOUSE_UP,Drop);
label_5.addEventListener(MouseEvent.MOUSE_DOWN,Drag);
label_5.addEventListener(MouseEvent.MOUSE_UP,Drop);
label_1.buttonMode = true;
label_2.buttonMode = true;
label_3.buttonMode = true;
label_4.buttonMode = true;
label_5.buttonMode = true;
function Drag(event:MouseEvent):void
{
event.target.startDrag(true,rect);
feedback_txt.text="";
event.target.parent.addChild(event.target);
startX=event.target.x;
startY=event.target.y;
}
function Drop(event:MouseEvent):void
{
event.target.stopDrag();
var myTargetName:String="target" + event.target.name;
var myTarget:DisplayObject=getChildByName(myTargetName);
if (event.target.dropTarget!=null&&event.target.dropTarget.parent==myTarget){
feedback_txt.text="Well done! You have selcted the correct label and placed it in the recommended position on the package.";
feedback_txt.textColor = 0xCC0000
event.target.removeEventListener(MouseEvent.MOUSE_UP,Drop);
event.target.removeEventListener(MouseEvent.MOUSE_DOWN,Drag);
event.target.buttonMode = false;
event.target.x=myTarget.x;
event.target.y=myTarget.y;
counter++;
correct_txt.text=counter;
correct_txt.textColor = 0x0000ff
attempts++;
attempts_txt.text=attempts;
attempts_txt.textColor = 0x0000ff
}else{
feedback_txt.text="Your attempt is not quite correct. You have either selected the incorrect label or placed it in the wrong position. Please try again.";
event.target.x = startX;
event.target.y = startY;
attempts++;
attempts_txt.text = attempts;
}
if (counter==5){
feedback_txt.text="Well done! You have correctly placed all 5 labels";
percentage_txt.text ="Based on your attempts, you have scored "+Math.round ((counter/attempts) *100)+" %";
percentage_txt.textColor = 0x0000ff
}
}
The easiest way to detect when a label is on another label is by using hittest in an enter frame event listener.
stage.addEventListener(Event.ENTER_FRAME, hit_test);
function hit_test(e:Event):void{
if (label_1.hitTestObject(targetLabel_1)) {
trace("Label_1 is hitting targetlabel_1");
label_hit();
}
if (label_2.hitTestObject(targetLabel_2)) {
trace("Label_2 is hitting targetlabel_2");
label_hit();
}
}
When the hittest is activated, the trace text is shown and the function is called. To change the alphas of the labels, use the function being called by the hittest. For example:
function label_hit()
{
label_1.alpha = 0;
label_2.alpha = 0;
label_3.alpha = 0;
}
If you are trying to have conditions to when things can be dragged, seen, or hit tested, that function is also where you can take care of them. For example, If you don't want a label to be visible until the hittest, you have the alpha set to 0 until the function sets it to 100. If you don't want a label to be drageable until then, you create the listener inside the function instead of earlier.
function label_hit()
{
label_1.alpha = 100;
label_1.addEventListener(MouseEvent.MOUSE_DOWN,Drag);
label_1.addEventListener(MouseEvent.MOUSE_UP,Drop);
}
If you want hittests to occur only after other hittests have already occured, place them in conditions and have the conditions met in the functions.
stage.addEventListener(Event.ENTER_FRAME, hit_test);
function hit_test(e:Event):void{
if (label_1.hitTestObject(targetLabel_1)) {
trace("Label_1 is hitting targetlabel_1");
label_hit();
}
if(condition)
{
if (label_2.hitTestObject(targetLabel_2)) {
trace("Label_2 is hitting targetlabel_2");
label_hit();
}
}
function label_hit()
{
var condition = true;
}

Make an item disapear when the player touch 2 times the touch screen

I'm trying to make a flash game for a touch screen (android). My player can take some items and put them in his inventory.
The player can, then, click on a item in his inventory and drag it where he wants on the scene.
But, I'd like to make the item disapear of the screen when the player double tap on the screen.
For the moment, my player need just to taps one. (and it's frustrating as, if he hasn't put the item into the correct space and wants to corrects it by dragging the item again, it's disappearing since he clicks on a wrong space).
I know that I have to change my "removeDraggedItem" function, but I can't figure it out how to tell it that I want this function only if the player ahs double tap on the screen (not just one click)
Here's my code. If someone has an idea....
public function itemClick(e:Event):void{
inv.draggingItem = false;
var nameofMC:String;
var tempMC;
var draggedName:String = draggedItem.displayName;
if (draggedItem.lookTag)
draggedName = draggedName + draggedItem.lookTag;
if (newFriend){
nameofMC = "action_"+draggedName+"_"+newFriend.displayName;
//trace ("Looking for "+nameofMC);
try {
tempMC = getDefinitionByName(nameofMC);
//trace ("MC found.");
removeDraggedItem();
if (speech)
speech.dispatchEvent(new Event("stopTalking"));
tempMC = new tempMC;
playerAction = new PlayerAction(stageRef, draggedItem, newFriend, tempMC, false);
}
catch(e){
//trace ("No MC found. Checking for dialog option...");
try {
var tempData = linesData.dialog[newFriend.displayName].useObject[draggedItem.displayName];
if (tempData != "" && tempData != null){
//trace ("Dialog option found.");
removeDraggedItem();
alignPlayer();
if (speech)
speech.dispatchEvent(new Event("stopTalking"));
dialog = new Dialog(stageRef, newFriend, draggedItem, false);
}
}
catch(e){
//trace ("No dialog option found. Defaulting to player line.");
alignPlayer();
if (speech)
speech.dispatchEvent(new Event("stopTalking"));
var actionName:String = "Use_"+newFriend.displayName;
if (newFriend.lookTag)
actionName = actionName+newFriend.lookTag;
speech = new Speech(stageRef, draggedItem, actionName);
}
}
} else {
removeDraggedItem();
}
}
private function removeDraggedItem():void{
stageRef.removeEventListener(MouseEvent.MOUSE_MOVE, dragItem);
stageRef.removeEventListener(Event.ENTER_FRAME, itemHitTest);
draggedItem.removeEventListener(MouseEvent.CLICK, itemClick);
stageRef.removeChild(draggedItem);
toolbar.useText.text = "";
if (stageRef.contains(this))
stageRef.removeChild(this);
Mouse.show();
Engine.playerControl = true;
}
On the first tap set a variable (eg screenTap) to true and set up a Timer for the maximum amount of milliseconds you will allow between taps in a double-tap. When the Timer expires reset screenTap to false.
Then, in your tap screen handler, check if screenTap is true. If so, remove the item.
Try to use MouseEvent.DOUBLE_CLICK instead of MouseEvent.CLICK.
Make sure double click is enabled for your stage :
stageRef.doubleClickEnabled=true;
then use :
stageRef.addEventListener(MouseEvent.DOUBLE_CLICK,removeDraggedItem);

What code in as-3 to use to allow seekbar to be moved by user?

I'm creating a simple music player which will play just one song. Stage timeline has got just one frame. There is main graphic for player which is movieClip_1 (it has 4 frames in it's own timeline) and that works ok. I've got button (button_2) which starts and pauses the song and the movieClip_1 (works ok). And i also have got a graphic (it's called tube - i have changed it to movie clip, it has got one frame inside it's timeline) as a seekbar component which I just want it to move correspondingly to channel.position of this song on the x axis which it does but gives me triple error of:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Debug points at this line:
tube.x = (chan.position/song.length) * 83;
I would really appreciate a tip regarding error and also what method to use in order for user to be able to navigate (with mouse) through song by moving tube on x axis and going to different part of song. I read about .startDrag and .stopDrag would that be good idea with my present code?
My code so far:
movieClip_1.stop();
var itsstoped:Boolean;
var youpausedit:Boolean;
var counter:Timer = new Timer(100);
var chan:SoundChannel;
var song:Sound = new Sound();
song.load(new URLRequest("wmc2.mp3"));
song.addEventListener(Event.COMPLETE, soundloaded);
function soundloaded(event:Event)
{
trace("Song has been loaded.");
}
counter.addEventListener(TimerEvent.TIMER,countcounter);
itsstoped = true;
youpausedit = false;
button_2.addEventListener(MouseEvent.CLICK, presser);
function presser(event:MouseEvent):void
{
if(itsstoped == true && youpausedit == true)
{
movieClip_1.gotoAndPlay(1);
chan = song.play(chan.position);
itsstoped = false;
}
else if(itsstoped == false)
{
movieClip_1.gotoAndStop(1);
chan.stop();
itsstoped = true;
youpausedit = true;
}
else if(itsstoped == true && youpausedit == false)
{
movieClip_1.gotoAndPlay(1);
chan = song.play();
counter.start();
itsstoped = false;
}
}
function countcounter(e:TimerEvent):void
{
trace(chan.position/song.length);
var percentage:uint = 100 * (chan.position / song.length);
trace(percentage);
if(percentage == 100)
{
movieClip_1.gotoAndStop(1);
itsstoped = true;
youpausedit = false;
counter.stop();
}
}
tube.buttonMode = true;
tube.useHandCursor = true;
addEventListener(Event.ENTER_FRAME, movewhenplay);
function movewhenplay(e:Event):void
{
tube.x = (chan.position/song.length) * 83;
}
Regarding the error, you're accessing an object that has yet to be instantiated, i.e. there is no data to fetch as it does not exist.
As for the searchbar, I'm guessing you're using some kind of mask for the search bar, as you're using its x position to display the song position. I would suggest you instead use its width parameter, and set the center point to the far left.
To be able to then search through the song, you can add a copy of this searchbar, place it on top of the first one. Set its alpha to 0, to make it invisible, but still clickable. Add mouseEvent listeners to that, and when the user clicks it, you can set the song to play from the position which you can calculate using mouseX relevant to the invisible search bar.
I.e. if you want the clicked position in percents
percent = (mouseX - invisiSearchBar.x) / invisiSearchbar.width