I'm trying to make a loop that won't crash my flash application. I want variable CN to go from 1 to 10, and then 10 turns into 1 (1,2,3,4,5,6,7,8,9,10,1....). This is what I have so far...
var CN:int = 1;
for(int CN = 1; CN<100; CN++);
NumberCounter.text = String(CN);
Please help. I don't get this at all :( I'm a novice programmer so a lot of things I do won't make much sense.
Your question is a bit unclear. Are you trying to go from 1 to 10 then from 10 to 1 once (20 steps) or to go back and forth between 1 and 10 in 100 steps ?
If it's the first, you can try something like this:
for(var i:int = 0, j:int = 0; i < 20; i++){
if(i < 10) j++;
else j--;
trace(j);//put this in your text field
}
if it's the second:
for(var i:int = 0; j:int = 1, k:int = 0; i < 100; i++){
if(i % 10 == 0) j *= -1; //every 10 steps flip (multiply by -1) the increment direction(increase/decrease)
k += j;//increment k based on j which will either increase or decrease
trace(k);//use this value
}
However the textfield will update right away. If you want to display this change in time you can use the ENTER_FRAME event to increment (rather than the for loop) or a tween engine to animate the value
For an infinite loop:
while(true){
for(var CN:int=1; CN <= 10; CN ++) NumberCounter.text = String(CN);
}
To do it 10 times, which looks like your code is hinting at:
for (int x= 0; x< 10; x++){
for(var CN:int=1; CN <= 10; CN ++) NumberCounter.text = String(CN);
}
Try this
if(CN>1)
{
var CN:int = 1;
for(int CN = 1; CN<10; CN++);
NumberCounter.text = String(CN);
}
else
{
}
var Numberofwins = 0;
CN.addEventListener(Event.ENTER_FRAME, checkFrame);
function checkFrame(event:Event):void{
if(CN.currentFrame == 11){
CN.gotoAndPlay(1);
}
}
import flash.events.MouseEvent;
Submit.addEventListener(MouseEvent.CLICK, CheckIf8);
function CheckIf8(event:MouseEvent):void
{
if(CN.currentFrame == 8)
{
Numberofwins++;
trace (Numberofwins);
Scorebox.text = String(Numberofwins);
}
else
{
gotoAndStop("Loose1");
}
}
This was my solution
Related
The assignment is the spawn several light and dark feathers according to score points from a quiz. The light feathers symbolize the correct points (light_feather), and the dark feather are the incorrect points (dark_feather) (Each are being tracked). All the feathers are supposed to line up on one line, meaning first light feathers, followed by the dark feathers. I got the quiz dynamics figured out, and the function I have posted here is only for when they press end quiz.
var light_feather:LightFeather = new LightFeather();
var dark_feather:DarkFeather = new DarkFeather();
var good_answers:uint = 0;
var bad_answers:uint = 0;
function avsluttFunc (evt:MouseEvent)
{
var sum_LightFeatherX:Number = 0;
for (var i = 0; i < good_answers; i++) {
addChild(light_feather);
light_feather.x += 12 + (i*16);
light_feather.y = 0;
trace("Lys X-verdi: " + light_feather.x);
sum_LightFeatherX += Number(light_feather.x);
return sum_LightFeatherX;
}
trace(sum_LightFeatherX);
dark_feather.x += sum_LightFeatherX;
for (var j = 1; j <= bad_answers; j++) {
addChild(dark_feather);
dark_feather.x += 12 + (j*16);
dark_feather.y = 0;
trace("Mørk X-verdi: " + dark_feather.x);
}
/*
//Resetter poengsummen
good_answers = 0;
bad_answers = 0;
*/
}
You can do what you are looking for using only one for loop, take a look :
var good_answers:uint = 2;
var bad_answers:uint = 4;
function avsluttFunc(evt:MouseEvent)
{
for (var i:int = 0; i < good_answers + bad_answers; i++) {
var feather:DisplayObject = i < good_answers ? new LightFeather() : new DarkFeather();
feather.x += 12 + i * (feather.width + 1);
feather.y = 0;
addChild(feather);
}
}
This code example will create 4 DarkFeather instances next to 2 LightFeather ones.
Edit :
How to add your objects to an array ?
// feathers array should be accessible for both codes (adding and removing objects)
var feathers:Array = [];
for (var i:int = 0; i < good_answers + bad_answers; i++) {
var feather:DisplayObject = i < good_answers ? new LightFeather() : new DarkFeather();
addChild(feather);
feathers.push(feather);
}
then to remove them from the stage, you can do for example :
for (var i:int = 0; i < feathers.length; i++) {
var feather:DisplayObject = DisplayObject(feathers[i]);
feather.parent.removeChild(feather);
}
Hope that can help.
I am new to Action Script programming Language, and I want to create the following number pattern:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
How can I code this pattern in Action Script Language?
I'm surprised anyone's starting to learn ActionScript (assuming you're using it for Flash) when the rest of the planet is moving toward HTML5 but, to each their own :-)
The algorithm you want for that sequence is relatively simple, and should be simple to convert into any procedural language:
for limit = 5 to 1 inclusive, stepping by -1:
for number = 1 to limit inclusive, stepping by 1:
output number
output newline
That's basically it. Based on my (very) limited exposure to AS3, that would come out as something like:
for (var lim:Number = 5; lim > 0, lim--) {
for (var num:Number = 1; num <= lim, num++) {
doSomethingWith(num);
}
}
var x:int = 5;
while(x > 0) {
var output:String = "";
for(var i:int = x; i > 0; i--) {
output = i + " " + output;
x--;
}
trace(output);
}
//Here the variable to build the string to "print"
var str:String="";
//We need two loops
//The first to change the row
for (var lim:int= 5; lim > 0; lim--) {
//Reset the string each new row
str="";
//And here the scond to build the string to "print"
for (var num:int= 1; num <= lim; num++) {
str+=num+" ";
}
//Here "print" the string
trace(str);
}
This is my code to create a pattern like that:
for (var i:int = 5; i >= 1; i--) { // represent the row
trace("1"); // print first element
for (var j:int = 2; j <= i; j++) { // represet the line
trace(" " + j.toString()); // print space and a number
}
trace("\n"); //next line
}
As I said: Is there any difference (in performance) between these two for loop?
Between this:
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
var currentObject:DisplayObject = displayObjects_[i];
currentObject.width = newWidth;
}
and this:
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
displayObjects_[i].width = newWidth;
}
I tested them before. The result said the first one was faster but I don't know if I did it right.
I know it's not really an answer to your question, but if you're looking for fastest way to iterate through this array you should do:
for each(var currentObject:DisplayObject in displayObjects_) {
currentObject.width = newWidth;
}
I tried this out on a bare-bones project using SDK 4.6. This is the code.
public class Main extends Sprite
{
public function Main()
{
var displayObjects_:Array = [];
for (var i:int = 0; i < 1000000; i++)
{
displayObjects_.push(new Sprite());
}
var start:int = getTimer();
for (i = 0; i < displayObjects_.length; i++)
{
var currentObject:Sprite = displayObjects_[i];
currentObject.width = 100;
}
var end:int = getTimer()
trace(end, start, end - start);
start = getTimer();
for (i = 0; i < displayObjects_.length; i++)
{
displayObjects_[i].width = 100;
}
end = getTimer()
trace(end, start, end - start);
}
}
These are the results.
Done(0)
[Starting debug session with FDB]
16703 16250 453
17141 16703 438
Like I said, it would be very surprising to see any difference between the two. You'll probably see more improvements through using Vector instead of Array. Otherwise, this stuff is too mundane to fuss over.
As I know, Actionscript compiler automatically moves variable definitions to the begin of the function. So, this loop doesn't declare variable each time, first sample the same that:
var currentObject:DisplayObject;
var n:int = displayObjects_.length;
for (var i:int = 0; i < n; i++)
{
currentObject = displayObjects_[i];
currentObject.width = newWidth;
}
So, I think the difference only in one additional variable declare and it willn't affects performance. But 3vilguy's example is better.
I'm having trouble finding a way to do an array that doesn't make me type all the numbers , for example :
(i found this code online)
var array:Array=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
var odds:Array=[],
evens:Array=[],
odds_sum:int=0,
evens_sum:int=0
;
for(var i:int = 0; i < array.length; i++){
if(array[i] % 2 == 1){
odds.push(array[i]);
odds_sum += array[i];
} else {
evens.push(array[i]);
evens_sum += array[i];
}
}
trace(odds);
trace(odds_sum);
trace(evens);
trace(evens_sum);
i wanted the code to trace all the numbers between 1 and 100 (including the 1 and 100) but don't have to type them all down.
Help solving this would be much appreciated
Your question is ambiguous , If you simply need all the numbers between 1 and 100 you simply do
for(var i:int = 1; i <= 100; i++){
trace(i);
}
or if you want them in your array
var arr:Array = new Array();
for(var i:int = 1; i <= 100; i++){
arr.push(i);
}
or if you want odd and even numbers in separate array
var odds:Array = new Array();
var evens:Array = new Array();
for(var i:int = 1; i <= 100; i++){
if(i%2==0)
evens.push(i);
else
odds.push(i);
}
where the array odds/even have appropriate numbers between 1 and 100.
I want to check for a straight combination in a poker game.
So, I have this array: var tempArr:Array = new Array;
I have this for sorting the array:
for (i = 0; i < 7; i++)
{
tempArr[i] = pValue[i];
}
tempArr.sort( Array.NUMERIC );
pValue is the value of the cards, it's have range from 2 to 14.
So, if I have this Array: tempArray = [2,3,3,4,5,5,6];
How can I check if I have a straight combination in my hand?
Set a bucket array to save if you got a card in hand
var t:Array = [];
//t[2] = 1;mean you have 2
// t[3] = 0;mean you don't have 3
//first initialize t
for(var i:int = 0; i < 15; i++)
{
t[i] = 0;
}
//then set the values from tempArray
for (var j:int = 0; j < tempArray.length; j++)
{
t[tempArray[j]] = 1;
}
//if you have a straight combination in your hand
//you will get a k that t[k] & t[k-1]& t[k-2] & t[k-3] & t[k-4] == 1
var existed:boolean = false;//the flag save if you got a straight combination
for (var k:int = t.length - 1; k >= 4; k--)
{
if (t[k] == 0)
{
continue;
}
if ((t[k] & t[k-1] & t[k-2] & t[k-3] & t[k-4]) == 1)
{
existed = true;
break;
}
}