Flex- Action Script programming Language - actionscript-3

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
}

Related

How to walk a game character using mouse event in Adobe Animate CC AS3?

This question is related to my previous post, “TypeError: Error #1010: A term is undefined and has no properties” in AS3 because as I mentioned there, I'm creating an Android Game for our thesis. Now, I have a spritesheet of a character in the link: sprite character, I'm using this in the game. I'm researching on how to walk a character, I found one at a website, it actually works but unfortunately, it fails because the character didn't actually walk. I have no idea on what code will be place there. Either I will walk a character by clicking mouse or I will create a button then click on it to walk a character. What would be the code can I use for that? Any help will be appreciated.
P.S. In my previous post, I'm creating a code from timeline but now I transfer it to Actionscript file because of some errors.
EDIT:
Here's my code of the character:
forward.addEventListener(MouseEvent.CLICK, ppap);
function ppap(event:MouseEvent):void{
gril.x += mouseX;
gril.y += mouseY;
gril.gotoAndStop('i');
gameloop();
}
function gameloop(): void {
for (var o = 0; o > 5; o++) {
if (linya.hitTestObject(gril)) {
o++;
gotoAndStop(2);
scorer.visible = true;
timer.visible = true;
}
}
}
And the line: gril.gotoAndStop('a'); where the character is standing.
The gril is the instance name of a character. When it reaches to linya, the question will appear. Thanks!
Let's walk through your broken game loop
function gameloop(): void {
for (var o = 0; o > 5; o++) { //sets o to 0, loops as long as o > 5 (which it isn't, since we just set it to 0;
if (linya.hitTestObject(gril)) {
o++; //this also adds 1 to o
gotoAndStop(2);
scorer.visible = true;
timer.visible = true;
}
// if this part ever executed, it would add 1 to o
}
}
Do you see the problem? This for loop will not execute even once since 0 < 5
Instead it should be
function gameloop(): void {
for (var i = 0; i < 5; i++) {
if (linya.hitTestObject(gril)) {
gotoAndStop(2);
scorer.visible = true;
timer.visible = true;
break;
}
}
}
So here we have a functional (but pointless) for loop. It will work, but the first time through the loop it is going to result in the exact same thing as the second and third and fourth and fifth because changing the variable value by 1 isn't actually changing anything at all. You just telling the program to check the collision state 5 times. Well it does this 5 times before anything else can change. It checks it 5 times every game loop. Well I promise you nothing is moving while that for loop is running so why check it 5 times? I suggest stepping back and getting some help from your teacher or something because this seems way off. Sorry.
Right again #NealDavis and thank You for the comment!!!
I wrote my comment too quickly
Ascending loop from 0->4 (5 items):
for (var i:uint = 0; i < 5; i++){
trace("ascending = " + i);
}
Output:
ascending = 0
ascending = 1
ascending = 2
ascending = 3
ascending = 4
Descending loop from 4->0 (5 items):
for (var j:int = 4; j>=0; j--){
// j must be an int in this case, second mistake!
trace("descending = " + j)
};
Output:
descending = 4
descending = 3
descending = 2
descending = 1
descending = 0
My mistake. SRY
This is well explained in Looping reference
And in ActionScript 3 fundamentals: Loops reference
Shame on me!!! ;)
I'm a noob! :D
So I deleted my comment ;)
WOW, I'm so sorry about this mistake!!!
// You may also create Vectors to store the values.
var ascending:Vector.<uint> = new Vector.<uint>;
var descending:Vector.<uint> = new Vector.<uint>;
for (var k:uint = 0; k < 5; k++){
ascending.push(k+1);
}
trace("ascending Vector.<uint> = " + ascending);
// Output : ascending Vector.<uint> = 1,2,3,4,5
for (var l:int = 4; l >= 0; l--){
descending.push(l+1);
}
trace("descending Vector.<uint> = " + descending);
// Output : descending Vector.<uint> = 5,4,3,2,1
Or in an ascending loop :
trace("\nascending Vector.<uint> loop : ")
for(var m:String in ascending){
trace(m + " = " + ascending[m]);
}
Output :
ascending Vector.<uint> loop :
0 = 1
1 = 2
2 = 3
3 = 4
4 = 5
Or in a descending loop :
trace("descending Vector.<uint> loop : ")
for(var n:String in descending){
trace(n + " = " + descending[n]);
}
Output:
descending Vector.<uint> loop :
0 = 5
1 = 4
2 = 3
3 = 2
4 = 1

ActionScript How to access child Sprites

I have the following problem. Would appreciate if someone takes the pains to answer it.
Problem Description: I have a top level Sprite "TopLevel",
there is a Sprite under it "allRoutes",
there is child Sprite under it "Routes" and
there is another child under it "Route".
I want to access each level in a loop. I have tried doing it in ActionScript but not with success.
Here is the probable code (it is not compiled successfully):
package
{
import flash.display.Sprite;
public class TopLevel extends Sprite {
public function TopLevel() {
var allRoutes:Sprite = new Sprite();
for (i = 0; i < 3; i++) {
var routes:Sprite = new Sprite();
for (j = 0; j < 4; j++) {
var route:Sprite = new Sprite();
route.name = "Route:" + i + ", " j;
routes.addChild(route);
}
routes.name = "Routes:" + i;
allRoutes.name = "AllRoutes";
allRoutes.addChild(routes);
}
addChild(allRoutes);
trace (allRoutes.name);
for (i = 0; i < allRoutes.numChildren; i++) {
trace(allRoutes.getChildAt(i).name);
for (j = 0; j < allRoutes.getChildAt(i).numChildren; j++) {
trace(allRoutes.getChildAt(i).getChildAt(j).name);
}
}
}
}
}
I am expecting the following result:
AllRoutes
Routes-1
Route-1, 1
Route-1, 2
Route-1, 3
Route-1, 4
Routes-2
Route-2, 1
Route-2, 2
Route-2, 3
Route-2, 4
so on..
Hope you get the picture.
Thank you very much in advance.
thanks and regards
rr23850
Globally your code is fine, except some little things :
You forgot to declare i and j variables :
var i:int, j:int;
You forgot the concatenation operator before j in this line :
route.name = "Route:" + i + ", " + j;
If your are compiling with the strict mode, these instructions will fire errors :
allRoutes.getChildAt(i).numChildren
and
allRoutes.getChildAt(i).getChildAt(j).name
which can be in that case :
for (j = 0; j < Sprite(allRoutes.getChildAt(i)).numChildren; j++)
{
trace(Sprite(allRoutes.getChildAt(i)).getChildAt(j).name);
}
...
And as your for loops are starting by 0, you will get something like this :
AllRoutes
Routes:0
Route:0, 0
Route:0, 1
Route:0, 2
Route:0, 3
Routes:1
Route:1, 0
Route:1, 1
...
Hope that can help.

AS3 Array 1 to 100

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.

How to make a variable loop in actionscript 3

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

AS3: Vector item isn't spliced

Hello I am creating a system with a gun that shoots bullet.
The update function is processed this way:
var b:Bullet;
var l:uint = bulletList.length;
var i:uint;
for (i = 0; i < l; i++) {
b = bulletList[i];
b.sprite.x += b.vx;
b.sprite.y += b.vy;
if (b.sprite.x > 1200 || b.sprite.x < -100 || b.sprite.y < -1000) {
deleteBullet(b);
bulletList.splice(i,1);
}
}
public function deleteBullet(b:Bullet) {
b.sprite = null;
b = null;
}
When I shoot a bullet and it goes of the edge it generates an error, and sometimes it creates a new one but that one doesn't have any motion at all. This is the error I get:
RangeError: Error #1125: The index 1 is out of range 1.
You're getting that error because you're splicing your array while in a for loop.
instead of using 'l' as your parameter for the for loop, use bulletList.length directly as every iteration it will look at the CURRENT length which will reflect anything spliced out of it. You'll also need subtract your iterator when splicing as that shifts all future indexes down by one.
for (i = 0; i < bulletList.length; i++) {
b = bulletList[i];
b.sprite.x += b.vx;
b.sprite.y += b.vy;
if (b.sprite.x > 1200 || b.sprite.x < -100 || b.sprite.y < -1000) {
deleteBullet(b);
bulletList.splice(i,1);
i--;
}
}