how to getTime in actionscript 3 - actionscript-3

I want to convert code from as2 to as3.
timeFaktor = 4;
function timeReset() {
delete curretnTime;
delete showtTime;
currentTime = new Date();
showTime = new Date();
startingTime = false;
pauseTime = 0;
txt_Time.text = "t = "+0+" s";
}
function moveTime() {
delete currentTime;
currentTime = new Date();
if (!startingTime) {
starTime = currentTime.getTime();
startingTime= true;
}
newTime = Number(currentTime.getTime()-starTimeMulai)+Number(pauseTime);
secon = newSecon/1000;
time = (Math.round(detik*100))/100;
txt_time.text = "t = "+time+" s";
}

It's just saying that ketapel1 is not a function but you are treating it as one by trying to pass it parameters.
ketaple1(parameter1, parameter2, ...);
Flash sees this and says "son is trying to pass parameters to this thing like it's a function" and throws the error.
The second error is saying that Xo hasn't been defined. Which it looks like it hasn't. Define those values outside any other functions.
Since I don't know what Xo or ketaple is I can't help more than that

Related

Loading more than one child image AS3

I'm trying to load 2 images in to my flash file but getting an error - new to this AS3 malarkey any help would be appreciated!
Getting error: 5 1151: A conflict exists with definition request in namespace internal.
var myImage:String = dynamicContent.Profile[0].propImage.Url;
var myImage2:String = dynamicContent.Profile[0].prop2Image.Url;
var myImageLoader:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(enabler.getUrl(myImage));
myImageLoader.load(request);
myImageLoader.x =17;
myImageLoader.y = 0;
var myImageLoader2:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(enabler.getUrl(myImage2));
myImageLoader.load(request);
myImageLoader.x =17;
myImageLoader.y = 0;
if(this.currentFrame == 1) {
addChildAt(myImageLoader, 2);
}
if(this.currentFrame == 2) {
addChildAt(myImageLoader2, 2);
}
It's usually a good idea to move duplicate code into its own function instead of doing copy + paste:
function loadImage(file:String, x:Number, y:Number):StudioLoader{
var myImageLoader:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(file);
myImageLoader.load(request);
myImageLoader.x = x;
myImageLoader.y = y;
return myImageLoader;
}
addChild(loadImage(enabler.getUrl(myImage1),17,0));
addChild(loadImage(enabler.getUrl(myImage2),17,0));
That's not just giving you better structured code but also fixes your duplicate variable definition issue because what's defined locally inside a function stays inside the function.
This might provide some insight:
http://www.adobe.com/devnet/actionscript/learning/as3-fundamentals/functions.html
You can't create a new variable with the same name as an existing one, in your case I'm speaking about your URLRequest request, so to avoid this type of error you can do like this :
var myImageLoader2:StudioLoader = new StudioLoader();
// assing a new URLRequest to an existing var
request = new URLRequest(enabler.getUrl(myImage2));
// here you want use the myImageLoader2, not myImageLoader
myImageLoader2.load(request);
myImageLoader2.x =17;
myImageLoader2.y = 0;
Or :
var myImageLoader2:StudioLoader = new StudioLoader();
// create a new URLRequest var, so the name should be different
var request2:URLRequest = new URLRequest(enabler.getUrl(myImage2));
// here you want use the myImageLoader2, not myImageLoader
myImageLoader2.load(request2);
myImageLoader2.x =17;
myImageLoader2.y = 0;
Additionally, I notice in the second block that you declare myImageLoader2 but you then use the original myImageLoader to do the load request. So even if you do declare a new URLRequest, you wont get both images loaded.
Akmozo's solution has this corrected.

How to get a time as a variable in Flash CS6

I am trying to get a time that is in a text document to become a variable in a Flash CS6 AS3 project. I can't seem to find where the problem is and the error messages aren't really helping. The highlighted parts are the changed lines.
Here is the newest code:
this.onEnterFrame = function()
{
var StartTime:URLLoader = new URLLoader();
StartTime.dataFormat=URLLoaderDataFormat.VARIABLES;
StartTime.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event):void {
}
StartTime.load(new URLRequest("ResponseTime.txt"));
var today:Date = new Date();
var currentTime = today.getTime();
var targetDate:Date = new Date();
var timeLeft = e.data - currentTime;
var sec = Math.floor(timeLeft/1000);
var min = Math.floor(sec/60);
sec = String(sec % 60);
if(sec.length < 2){
sec = "0" + sec;
}
min = String(min % 60);
if(min.length < 2){
min = "0" + min;
}
if(timeLeft > 0 ){
var counter:String = min + ":" + sec;
time_txt.text = counter;
}else{
var newTime:String = "00:00";
time_txt.text = newTime;
delete (this.onEnterFrame);
}
}
Newest Error:
1120: Access of undefined property e. (Line 17).
First of all, this does nothing :
var StartTime();
It's not correct AS3 code.
Then, AS3 loaders being asynchronous, you must way for the loader to finish load so you can get your variable. I mean that all your code after StartTime.load(...) must be inside the function onLoaded.
This way, when the loader finish loading, you'll have you variable.
That say, URLVariable is NOT a loader. It is an object you can use to put your variable into, and feed them to a loader.
If you want to download some file, use URLLoader (with URLRequest). On this page, there is a good example on how you can do that (skip the part about the dataFormat, though). The date you're requesting will be available in the data property of the event, eg :
var timeLeft = e.data - currentTime;
I'm not asking where currentTime is from, since it's out of the scope of that question.
Good luck.

Show my timer time during my basic game

Im doing a little game in actionscript, and i have a timer that starts when the gamer starts and end when the game ends.
But i want to show in a textfield the value of my timer during the game like 1,2,3,4,5,6, etc.
How can i can acess that timer propriety to get the value?
My timer code is this:
startTime = (new Date().time);
endTime = (new Date().time);
public function testTime():void
{
const 5_min = 5 * 60 * 1000;
const 2_min = 2 * 60 * 1000;
var timeDiff:Number = endTime - startTime;
if (timeDiff < 2_min) {
trace("Good!");
} else {
trace("Bad!");
}
}
And here (above) i create a txt field. Now, how i can show the timer value while playing the game?
var timer_txt:TextField;
pontuacao = new TextField();
timeDiff = 0;
timer_txt = new TextField();
timer_txt.text = String(timeDiff);
stage.addChild(timer_txt);
timer_txt.x = 470;
timer_txt.y = 320;
Well, first of all, in AS you cannot begin your variable names with a number, therefore this code should absolutely not compile (5_min, 2_min = WRONG!).
You have your startTime, which is ok. Now you will have to check the current time in some kind of event (Timer, EnterFrame) or interval (setInterval) and update the endTime value accordingly. Once you have it, you just count the difference and divide it by 1000 (I assume you don't want to show it in millis)
Something like this:
import flash.utils.setInterval;
var s:Number = new Date().getTime(); //start time
var e:Number; //end time
var d:Number; //difference
setInterval(function () {
e = new Date().getTime();
var oldDiff:Number = d;
d = int((e - s) / 1000);
if(oldDiff != d) trace(d + " seconds since launch");
},
100
);

AS3 - Yet another TypeError: Error #1009 Cannot acces

I am creating a "Space Invaders" game in AS3. Got everything working now, but keep ketting a #1009 error and i'm looking at it for quite a time now and i just don't see the problem. Some extra eyes could help out I think!
This is what the debugger says:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at testbestandkopie_fla::MainTimeline/winGame()[testbestandkopie_fla.MainTimeline::frame1:352]
at testbestandkopie_fla::MainTimeline/enemyHitTest()[testbestandkopie_fla.MainTimeline::frame1:338]
at testbestandkopie_fla::MainTimeline/onTick()[testbestandkopie_fla.MainTimeline::frame1:117]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
Below I will paste the code that has to do with the error:
1: Creating the AS Linkage names:
//Sound FX-------------------------------------------------------------------------
var startFX:Sound = new startGameSound();
var laserFX:Sound = new laserSound();
var explosionFX:Sound = new explosionSound();
var musicFX:Sound = new backgroundMusic();
var loseFX:Sound = new endGameSound();
var winFX:Sound = new winGameSound();
var SfxTransform = new SoundTransform();
var myChannelMisc:SoundChannel = new SoundChannel();
var myChannelMusic:SoundChannel = new SoundChannel();
var myChannelWin:SoundChannel = new SoundChannel();
var myChannelLose:SoundChannel = new SoundChannel();
//---------------------------------------------------------------------------------
2: [Line 117] The AS line 117 is the highlighted one:
//Handlers Functions---------------------------------------------------------------
function onTick(e:TimerEvent) { //A continuous run of some functions below.
moveCharacter();
moveEnemyField();
playerCollisionDetectWall();
enemyCollisionDetectWall();
enemyCollisionDetectWallBottom();
moveLasers();
enemyHitTest(); <---- line 117
}
3: [Line 338] The function enemyHitTest(); where it starts the winGame(); function:
function enemyHitTest() {
//For each of the three enemys
for (var i:int = 0; i < enemyArray.length; i++) {
//the each of the six lasers
for (var j:int = 0; j < 6; j++) {
//don't consider lasers that aren't in play:
if (laserArray[j].y > SpelerMC.y) continue;
if (enemyArray[i].visible && enemyArray[i].hitTestObject(laserArray[j])) {
score += 10;
myChannelMisc = explosionFX.play();
SfxTransform.volume = 0.3;
myChannelMisc.soundTransform = SfxTransform;
scoreTxt.text = score.toString();
trace("Invader nummer " + i + " neergeschoten!");
enemyArray[i].visible = false;
//Now we remove the laser when hitting.
laserArray[j].x = j * 70 + 100;
laserArray[j].y = 895;
}
else if(score == 660) {
//If you reach a score of 660 (66 enemy's x 10 = 660) you win the game.
winGame(); <---- Line 338
}
}
}
}
4: [Line 352] Where the winGame(); function run's after getting 660 points at the enemyHit.
function winGame() {
winScreen.visible = true;
gameTimer.stop();
//Stop the music.
myChannelMusic.stop();
//Start the "You Win" music.
myChannelWin = winFX.play();
SfxTransform.volume = 0.02;
myChannelWin.soundTransform = SfxTransform; <---- line 352
}
So as you can see, it run's through these functions. I've already checked if something is wrong with my file in the Library, but the AS Linkage name is exactly the same as the var I defined above. Maybe a few extra eyes can see what's going wrong here, and explain me why..
Thanks in advance!
As per the livedocs :
winFX.play() method may return null if you have no sound card or if you run out of available sound channels. The maximum number of sound channels available at once is 32.
Check if either of the issues above is applicable to you....
As Mark said, the class winGameSound is the culprit here. The call winFX.play() returns a null, not a sound channel. So you can not apply a sound transform to the null object.
The only info one can currently get is that the class inherits Sound class & returns null with play() call.

Closure problem? - passing current value of a variable

I'm trying to pass the current value of a variable when an a dynamically generated navigation 'node' is clicked. This needs to just be an integer, but it always results in the last node's value.. have tried some different methods to pass the value, a custom event listener, a setter, but I suspect it's a closure problem.. help would be appreciated ;-)
function callGrid():void {
for (var i:Number = 0; i < my_total; i++) {
var gridnode_url = my_grid[i].#gridnode;
var news_category= my_grid[i].#category;
var newstitle = my_grid[i].#newstitle;
var news_content = my_grid[i]..news_content;
var news_image = my_grid[i]..news_image;
var gridnode_loader = new Loader();
container_mc.addChild(gridnode_loader);
container_mc.mouseChildren = false;
gridnode_loader.load(new URLRequest(gridnode_url));
gridnode_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, gridLoaded);
gridnode_loader.name = i;
text_container_mc = new MovieClip();
text_container_mc.x = 0;
text_container_mc.mouseEnabled = false;
var textY = text_container_mc.y = (my_gridnode_height+18)*y_counter;
addChild(text_container_mc);
var tf:TextSplash=new TextSplash(newstitle,10,0,4 );
container_mc.addChild(tf);
tf.mouseEnabled = false;
tf.height = my_gridnode_height;
text_container_mc.addChild(tf);
var text_container_mc_tween = new Tween(text_container_mc, "alpha", Strong.easeIn, 0,1,0.1, true);
gridnode_loader.x = (my_gridnode_width+5) * x_counter;
gridnode_loader.y = (my_gridnode_height+15) * y_counter;
if (x_counter+1 < columns) {
x_counter++;
} else {
x_counter = 0;
y_counter++;
}
}
}
function gridLoaded(e:Event):void {
var i:uint;
var my_gridnode:Loader = Loader(e.target.loader);
container_mc.addChild(my_gridnode);
_xmlnewstarget = my_gridnode.name;
//||||||||||||||||||||||||||||||||||||||||
//when a particular grid node is clicked I need to send the current _xmlnewstarget value to the LoadNewsContent function...
//||||||||||||| ||||||||||||||||||||||||
my_tweens[Number(my_gridnode.name)]=new Tween(my_gridnode, "alpha", Strong.easeIn, 0,1,0.1, true);
my_gridnode.contentLoaderInfo.removeEventListener(Event.COMPLETE, gridLoaded);
my_gridnode.addEventListener(MouseEvent.CLICK, loadNewsContent);
}
function loadNewsContent(e:MouseEvent):void {
createNewsContainer();
getXMLNewsTarget();
news_category = my_grid[_xmlnewstarget].#category;
var tfnews_category:TextSplash=new TextSplash(news_category,20,16,32,false,false,0xffffff );
tfnews_category.mouseEnabled = false;
newstitle = my_grid[_xmlnewstarget].#newstitle;
var tftitle:TextSplash=new TextSplash(newstitle,20,70,24,false,false,0x333333 );
news_container_mc.addChild(tftitle);
tftitle.mouseEnabled = false;
news_content = my_grid[_xmlnewstarget]..news_content;
var tfnews_content:TextSplash=new TextSplash(news_content,20,110,20,true,true,0x333333,330);
news_container_mc.addChild(tfnews_content);
tfnews_content.mouseEnabled = false;
news_image = my_grid[_xmlnewstarget].#news_image;
loadNewsImage();
addChild(tfnews_category);
addChild(tftitle);
addChild(tfnews_content);
var news_container_mc_tween = new Tween(news_container_mc, "alpha", Strong.easeIn, 0,1,0.3, true);
news_container_mc_tween.addEventListener(Event.INIT, newsContentLoaded);
}
I'm not going to try to read your code (try to work on your formatting, even if it's just indenting), but I'll provide a simplified example:
for (var i = 0; i < my_total; i++) {
var closure = function() {
// use i here
}
}
As you say, when closure is called it will contain the last value of i (which in this case would be my_total). Do this instead:
for (var i = 0; i < my_total; i++) {
(function(i) {
var closure = function() {
// use i here
}
})(i);
}
This creates another function inside the loop which "captures" the current value of i so that your closure can refer to that value.
See also How does the (function() {})() construct work and why do people use it? for further similar examples.
Umm, as mentioned above, the code is a bit dense, but I think you might have a bit of type conversion problem between string and integers, is the "last value" always 0? try making these changes and let me know how you get on.
// replace this gridnode_loader.name = i;
gridnode_loader.name = i.toString();
// explictly type this as an int
_xmlnewstarget = parseInt(my_gridnode.name);
// replace this: my_tweens[Number(my_gridnode.name)] = new Tween(......
my_tweens[parseInt(my_gridnode.name)] = new Tween();
Oh and I think it goes without saying that you should massively refactor this code block once you've got it working.
Edit: after further study I think you need this
//replace this: my_gridnode.addEventListener(MouseEvent.CLICK, loadNewsContent);
var anonHandler:Function = function(e:MouseEvent):void
{
loadNewsContent(_xmlnewstarget);
};
my_gridnode.addEventListener(MouseEvent.CLICK, anonHandler);
Where your loadNewsContent has changed arguements from (e:MouseEvent) to (id:String)
Firstly, you do not need to call addChild for the same loader twice (once in callGrid) and then in (gridLoaded). Then you can try putting inside loadNewsContent: news_category = my_grid[int(e.target.name)].#category;instead of news_category = my_grid[_xmlnewstarget].#category; As _xmlnewstarget seems to be bigger scope, which is why it is getting updated every time a load operation completes.