PDA

View Full Version : Exlcuding a specific value from an Array and changing the background color



Sux2bu
November 4th, 2009, 01:26 PM
First of all, hi!
I'm Sux2bu, new to the forum and partially new to some advanced feats from AS3 :)

So, 8 weeks ago I started with my GameDevelopment branch of the MediaTechnology tree at my school, and I am currently in my 1st week of the 2d Semester (or Period, as we call it here), and AS3 is introduced this time (last Period were GameMaker and XNA).
There's a problem though.. I have to work with Arrays now, and last time I worked with Arrays was during PHP last year; almost half a year ago >_<"

Here's what we have to do for the 1st Assignment:
Make a sort of lottery system for Christmass; Make sure names aren't drawn more than once, and that someone can't draw him/herself.

This is the code for the first assignment:

package{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.MouseEvent;

public class Opdracht1 extends MovieClip{

public function Opdracht1():void{
//Buttonmodes
Loten_btn.buttonMode = true;
//Event Listeners
Loten_btn.addEventListener(MouseEvent.CLICK, LotenVanNamen);
//variabelen
var TextVisible:Boolean = false;
var ArrayText:TextField = new TextField();
var Namen:Array = new Array();
//Waardes van de variabelen
/*Start*/Namen = ["Kyrsie","Troy","Chris","Linde","Martin"];
//LootNamen = ["Kyrsie","Troy","Chris","Linde","Martin"];
ArrayText.x = 50;
ArrayText.y = 100;
ArrayText.width = 550;
ArrayText.height = 400;
ArrayText.text = "";

//Tekst laten verschijnen
function LotenVanNamen(event:MouseEvent){
if(TextVisible != true){
TextVisible = true;
addChild(ArrayText);
}else{
TextVisible = false;
removeChild(ArrayText);
}
}
for(var namen:int = 0; namen < Namen.length; namen++){
//Getal kiezen en direct in een String zetten
var LootGetal:Number = Math.round(Math.random() * (Namen.length-1));
var LootNaam:String = String(LootGetal);
var ArrayNaam:String = String(namen);
if(ArrayNaam != LootNaam){
//Naam aan een ArrayGetal koppelen
if(namen == 0){
ArrayNaam = "Kyrsie";
}
if(namen == 1){
ArrayNaam = "Troy";
}
if(namen == 2){
ArrayNaam = "Chris";
}
if(namen == 3){
ArrayNaam = "Linde";
}
if(namen == 4){
ArrayNaam = "Martin";
}
//Naam aan een LootGetal koppelen
if(LootGetal == 0){
LootNaam = "Kyrsie";
}
if(LootGetal == 1){
LootNaam = "Troy";
}
if(LootGetal == 2){
LootNaam = "Chris";
}
if(LootGetal == 3){
LootNaam = "Linde";
}
if(LootGetal == 4){
LootNaam = "Martin";
}
ArrayText.appendText(ArrayNaam + " maakt een surprise voor " + LootNaam + "\n" + "\n");
}else{
if(ArrayNaam == LootNaam){
ArrayText.appendText("FAAL!!" + "\n" + "\n");
}
}
}
}
}
}The variable names, function names and texts are in Dutch.
LootGetal = DrawNumber; The Number Drawed that will later on be transfered into a name.
LootNaam = DrawName; The name, converted from the Drawn number.
ArrayNaam = ArrayName; the name from the Array that will be place in order from 0 to 4.

Right now, the problems I am encountering is the fact that when ArrayNaam equals LootNaam a new name is drawn (not in the above code, but in an earlyer code), but there was still the problem that something like Linde draws Linde is possible (duhr.. -_-").
So, how do I exclude a specific value so that this can not happen?

That would bring me a good step in another direction :)

The second problem is that I have to make sure names aren't drawn more than once. So something like:
Linde draws Martin,

Troy draws Martin

happens.

3rd; It bothers me that I use such an insaneley huge piece of code just to give numericals a String value (the endless list of if() statements). Any easyer way? I'm sure there has to be one.. I have not found one however.

That pretty much sums up Assignment 1 :)

Now, on to the 2d Assignment:
Work with external files (.txt files) to load in variables, pictures and such.
Make sure a picture is loaded this way - works. Easymode ;D
Make sure the background color is changed through an external (.txt) file.
code:

package{
import flash.display.*;
import flash.events.*;
import flash.net.*;

public class Opdracht2 extends MovieClip{

var LaadFoto:Loader = new Loader();
var ColorChange:Boolean = new Boolean;

public function Opdracht2():void{
//Variabelen laden
var LaadVari:URLLoader = new URLLoader();
LaadVari.dataFormat = URLLoaderDataFormat.VARIABLES;
LaadVari.addEventListener(Event.COMPLETE, GeladenVari);
LaadVari.load(new URLRequest("Opdracht2Vari.txt"));
//Foto's laden
LaadFoto.contentLoaderInfo.addEventListener(Event. COMPLETE, GeladenFoto);
LaadFoto.load(new URLRequest("4Chan.gif"));
//Button eigenschappen
Color_BTN.addEventListener(MouseEvent.CLICK, ColChange);
ColorChange = false;
//BG eigenschappen
var LaadKleuren:URLLoader = new URLLoader();
LaadKleuren.dataFormat = URLLoaderDataFormat.VARIABLES;
LaadKleuren.addEventListener(Event.COMPLETE, GeladenKleur);
LaadKleuren.load(new URLRequest("Opdracht2Kleuren.txt"));
Color_BTN.buttonMode = true;
}
public function GeladenVari(evt:Event):void{
LaadFoto.x = evt.target.data.Xpositie;
LaadFoto.y = evt.target.data.Ypositie;
}
public function GeladenFoto(evt2:Event):void{
addChild(LaadFoto);
}
public function ColChange(evt3:MouseEvent):void{
if(ColorChange != true){
ColorChange = true;
}else{
ColorChange = false;
}
}
public function GeladenKleur(evt4:Event):void{
//Kleur Bepalen
if(ColorChange != true){
graphics.lineStyle(0, 0xFFFFFF);
graphics.beginFill(evt4.target.data.Kleur1);
graphics.drawRect(0,0, 550, 400);
graphics.endFill();
}else{
if(ColorChange == true){
graphics.lineStyle(0, 0xFFFFFF);
graphics.beginFill(evt4.target.data.Kleur3);
graphics.drawRect(0,0, 550, 400);
graphics.endFill();
}
}
}
}
}Browsing Google (which is your best friend, and also led me here), I found out that the backgroundcolor can't be changed during run-time; thus several sites advised to make an existing object and use the gotoAndStop()] Method to switch between colours.
That seemed kind of weak.. so I pulled the beginFill method from the dusty shelf from last year, and came up with the drawing a square (background) through code; makes it easyer ^_^ less switching between screens o-o

However, the final if() - else statement doesn't work. The start value of the if(ColorChange != true) works (which will define the start color). Switching the value makes the background change; so I can state that it actually works according to the assignment. Only problem is; when I press the button, changin the Boolean to true, the color doesn't change from Kleur(Color)1 to Kleur3.

This is an alternative I used, but comes with the same outcome:

public function GeladenKleur(evt4:Event):void{
//Kleur Bepalen
graphics.lineStyle(0, 0xFFFFFF);
if(ColorChange != true){
graphics.beginFill(evt4.target.data.Kleur1);
}else{
if(ColorChange == true){
graphics.beginFill(evt4.target.data.Kleur3);
}
graphics.drawRect(0,0, 550, 400);
graphics.endFill();
}So, any solution to this?

If I can get help with these two assignments, they're complete.
And before you ask; No. It does not help asking my teacher because all I get out of him is "Sorry, can't tell. Otherwise I'd tell the answer" with a look in his eyes as if he doesn't know what's going on.

Greetings,

Sux2bu

IQAndreas
November 4th, 2009, 01:50 PM
Isn't the point of getting this assignment to help you learn?

I could give you code, but that goes against all of my principles, and won't help you learn. However, (for those questions I understood) I'll give you a few pointers.

First, I would REALLY recommend that you read up on Arrays. There is this great chapter in Adobe's free online "Programming ActionScript 3.0" book. If you read that, you will learn a lot, but this is the chapter specifically on arrays:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fdc.html



3rd; It bothers me that I use such an insaneley huge piece of code just to give numericals a String value (the endless list of if() statements). Any easyer way? I'm sure there has to be one.. I have not found one however.
Second, um, yes. That's exactly why you use Arrays. Looks like someone wasn't paying attention in class.

READ THE CHAPTER!! After that, realize that you can access properties in an array by passing in a number, like "namen[2]" or "namen[6]", or whatever number you want to access.

Here it is illustrated in actual code:

for(var namen:int = 0; namen < Namen.length; namen++){
//Getal kiezen en direct in een String zetten
var LootGetal:Number = Math.round(Math.random() * (Namen.length-1));

//You don't need to convert numbers to strings in order to use them in "if" statements,
//so I removed all this
//var LootNaam:String = String(LootGetal);
//var ArrayNaam:String = String(namen);

if(ArrayNaam != LootNaam){

//Big, long, scary chunk of code gone! :)

//This is all that remains
ArrayText.appendText(Namen[LootGetal] + " maakt een surprise voor " + LootNaamen[namen] + "\n" + "\n");

}else{
if(ArrayNaam == LootNaam){
ArrayText.appendText("FAAL!!" + "\n" + "\n");
}
}
}
CURSES! You exploited my weakness and made me write code anyway. As a punishment, I won't tell you how to fix anything else until you read the chapter, and then post the fixed code here so I know you know the information!

And don't think you can just use the code I have you! I put an error in it, so it won't work as expected!! (Muahaha) Your job is to find this bug, and fix it! (I believe in you)

Sorry for being a jerk, but if you want my help, you will have to work for it.

micken
November 4th, 2009, 01:52 PM
I will help with assignment 1:

A good way to solve problems is to think of how you would do them in reality. From the way you describe your assignment it looks to me like you are making a program that draws names out of a hat. So you got the first step right, to make an array of names to act as the hat. Now when you take a name out of the hat, it is no longer there. How to accomplish this with ActionScript? like this:


Namen.splice(LootGetal, 1);


This will go to the index LootGetal in the array Namen and remove 1 item. Now that the name is no longer in the array it will not get drawn again.

Now to fix the problem of someone drawing themself, you can modify your random number generation like so:


var LootGetal:Number = NaN;
while(LootGetal == NaN)
{
LootGetal = Math.round(Math.random() * (Namen.length-1));
if (LootGetal == namen) { LootGetal = NaN; }
}



As for your second assignment you have nothing that calls GeladenKleur after you click the button.

Sux2bu
November 4th, 2009, 01:56 PM
@IqAndreas
Yes, it is to help me learn. But if the reader states Find out by yourself how to.., we are left alone. Or well, I get that feeling.
And I ALWAYS pay attention during class, but something like exluding a specific value from an Array was NOT explained.. other than to splice and slice an array -_-" But when I use those two functions in whatever way, some lines of text don't even show up o-o
Take this for examle:
Without slice and dice.. eurhm.. splice
5/5 text lines show up.
With slice and splice
5/5 lines show up when the code deems it to.
So I sometimes get 1/5 lines -_-"

anyway, I'ma go read the Adobe thingy, and try to fix your code.

Thanks for the reply ^_^

@micken
I don't have anything that calls GeladenKleuren?
that's weird.. I thought I did - ahwell, time to find that out.

Thanks ;D

**EDIT**
I finished Assignment 2 ^_^
had to change

public function ColChange(evt3:MouseEvent):void{
if(ColorChange != true){
ColorChange = true;
}else{
ColorChange = false;
}
}
public function GeladenKleur(evt4:Event):void{
//Kleur Bepalen
if(ColorChange != true){
graphics.lineStyle(0, 0xFFFFFF);
graphics.beginFill(evt4.target.data.Kleur1);
graphics.drawRect(0,0, 550, 400);
graphics.endFill();
}else{
if(ColorChange == true){
graphics.lineStyle(0, 0xFFFFFF);
graphics.beginFill(evt4.target.data.Kleur3);
graphics.drawRect(0,0, 550, 400);
graphics.endFill();
}
}
into

public function ColChange(evt3:MouseEvent):void{
//Variabelen aanmaken
var LaadKleuren:URLLoader = new URLLoader();
LaadKleuren.dataFormat = URLLoaderDataFormat.VARIABLES;
LaadKleuren.addEventListener(Event.COMPLETE, GeladenKleur);
LaadKleuren.load(new URLRequest("Opdracht2Kleuren.txt"));
//Rectangle tekenen
graphics.lineStyle(0, 0xFFFFFF);
//Functie aanmaken
function GeladenKleur(evt4:Event):void{
if(ColorChange != true){
ColorChange = true;
graphics.beginFill(evt4.target.data.Kleur1);
}else{
ColorChange = false;
graphics.beginFill(evt4.target.data.Kleur2);
}
}
graphics.drawRect(0,0, 550, 400);
graphics.endFill();

micken
November 4th, 2009, 02:30 PM
You only call it once after your loader finishes (via the event listener).
Also you might want to read up about Dictionaries. Most problems you can solve with arrays can be solved easier with dictionaries.