image array and .src - image not changing - arrays

I have created an array which is being used to store a series of .gif images and I'm just trying to test everything out by using document.getElementById to change the .src value but when I change it and load the page the image stays the same as it was before.
function setImage()
{
var images = new Array();
images[0] = anemone.gif;
images[1] = ball.gif;
images[2] = crab.gif;
images[3] = fish2.gif;
images[4] = gull.gif;
images[5] = jellyfish.gif;
images[6] = moon.gif;
images[7] = sail.gif;
images[8] = shell.gif;
images[9] = snail.gif;
images[10] = sun.gif;
images[11] = sunnies.gif;
images[12] = whale.gif;
var slots = new Array();
slots[0] = document.getElementById("slot" + 0);
slots[1] = document.getElementById("slot" + 1);
slots[2] = document.getElementById("slot" + 2);
slots[0].src = "snail.gif";
document.getElementById('slot0').src = images[0];
alert(images.length);
}
I can't understand why the image wont change, but I know it has to be something very simple. I've been wasting hours trying to get this one thing to change but nothing works. can anyone please point out the error of my ways?

There are a couple of issues with your code:
Your filenames need to be Strings, so they'll have to be quoted (also you can simplify the Array creation):
var images = ['anemone.gif', 'ball.gif', 'crab.gif', 'fish2.gif', 'gull.gif', 'jellyfish.gif', 'moon.gif', 'sail.gif', 'shell.gif', 'snail.gif', 'sun.gif', 'sunnies.gif', 'whale.gif'];
Also make sure you are getting your slot-elements right, quote all the attributes like:
<img id="slot0" class="slot" src="crab.gif" width="120" height="80">
When you create the slots-Array you can do it like this (no need to concat the ID string):
var slots = [document.getElementById('slot0'), document.getElementById('slot1'), document.getElementById('slot2')];
Finally make sure you call your function when the document has loaded / the DOM is ready. If you don't want to use a framework like jQuery your easiest bet is probably still using window.onload:
window.onload = setImage; //note that the parens are missing as you want to refer to the function instead of executing it
Further reading on Arrays, window.onload and DOMReady:
https://developer.mozilla.org/de/docs/DOM/window.onload
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array
javascript domready?

Related

How can I access an element from an array?

var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1Ow_rhF3sibKL3OAQRXpK6LLZDjMOhW-5DKyWWiN5iZg/edit#gid=638513192');
var data = SpreadsheetApp.setActiveSheet(ss.getSheetByName('Graficos'));
Logger.log(SpreadsheetApp.getActiveSpreadsheet());
var ergo = data.getRange(3,2,4,1);//B3:B6 '
My guess to access the elements was to call var i = ergo[0]; but it didn't work. Do I have to declare ergo using a different syntax?
You need to use getValues() on the range, which will return a 2-dimensional array.
var ergo = data.getRange(3,2,4,1).getValues();
Also, you're not coding efficiently as you're essentially duplicating actions in your first two lines. Take a look at this refactoring:
function test() {
var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1Ow_rhF3sibKL3OAQRXpK6LLZDjMOhW-5DKyWWiN5iZg/edit#gid=638513192');
var data = ss.getSheetByName('Graficos');
ss.setActiveSheet(data); // There doesn't seem to be a need to do this, so maybe delete this line
var ergo = data.getRange(3,2,4,1).getValues(); //B3:B6
}

Trying to modify a script to accept an array

I found a snowing image script on the web (http://www.jqueryrain.com/?GKBtfF4Q) and I am trying to modify the script to have multiple images.
So far, I figured out that it's necessary to change:
$(document).snow({ SnowImage: "snow.gif" });
to
$(document).snow({ SnowImage:[ "snow.gif", "2.gif", "3.gif", "4.gif"] });
but I am not sure how to make the script accept an array. Any suggestions would be much appreciated! (full disclosure: I am a coding noob)
Edit: pulled the script into JSFiddle: http://jsfiddle.net/px6w1xdm/
function __ShowSnow(settings)
{
var snowsrc = settings.SnowImage;
var no = settings.Quantity;
var dx, xp, yp; // coordinate and position variables
var am, stx, sty; // amplitude and step variables
var i;
etc.
Add this in the loop (above var flake):
var randno = Math.floor(snowsrc.length*Math.random());
var randsnowsrc = snowsrc[randno];
Then change snowsrc to randsnowsrc in this line:
flake.append("<img src='" + randsnowsrc + "'>");

AS3 saving from a timeline to a sharedObject (possible to place in an array)

I was hoping someone could offer a simple solution. I am trying to save a 'labeled' frame on the timeline by storing it as a SharedObject.
The user can flip between various different backgrounds on the stage by clicking a button - button one corresponds to background one, background 2 corresponds to btn two and so on... For your reference these backgrounds are stored in a sub timeline in a movieClip. Any tips on how to get this to store..?
I'm open to new theories as I'm not having a lot of success saving from the movieClip on the time line.
I have already posted a question similar to this but I was wandering if it was possible to store these frames in an array? Array1 = image1 Array2 = image2 and so making it easier to store. I'm guessing I would need to make a loadermodule to store these images on the stage as well.
Thanks
// SAVE FUNCTIONS ---------------------------------------
//---------------------------------------------------
//---------------------------------------------------
var mySO:SharedObject = SharedObject.getLocal("iDesign");
bones_mc.x = mySO.data.my_x;
bones_mc.y = mySO.data.my_y;
if (!mySO.data.my_y) {
bones_mc.x = 424;
bones_mc.y = 119;
}
//----
save_btn.addEventListener (MouseEvent.CLICK, clickersave);
function clickersave (e:MouseEvent):void {
mySO.data.my_x = bones_mc.x;
mySO.data.my_y = bones_mc.y;
mySO.data.mybut_x = btrfly_mc.x;
mySO.data.mybut_y = btrfly_mc.y;
mySO.data.mytig_x = tiger_mc.x;
mySO.data.mytig_y = tiger_mc.y;
mySO.data.mybow_x = pink_bow_mc.x;
mySO.data.mybow_y = pink_bow_mc.y;
mySO.data.myblkbow_y = pink_bow_mc.y;
mySO.data.myblkbow_x = pink_bow_mc.x;
// tears saved - - - - - - -
mySO.data.mytear_drop_mc_three_x = tear_drop_mc_three.x;
mySO.data.mytear_drop_mc_three_y = tear_drop_mc_three.y;
mySO.data.mytear_drop_mc_one_x = tear_drop_mc_one.x;
mySO.data.mytear_drop_mc_one_y = tear_drop_mc_one.y;
mySO.data.mytear_drop_mc_two_x = tear_drop_mc.x;
mySO.data.mytear_drop_mc_two_y = tear_drop_mc.y;
mySO.data.mytear_drop_mc_four_x = tear_drop_mc_four.x;
mySO.data.mytear_drop_mc_four_y = tear_drop_mc_four.y;
mySO.data.myframe = caseSwapper.currentFrame;
trace(caseSwapper.currentFrame)
mySO.flush ();
}
//caseSwapper.currentFrame = mySO.data.myframe;
tear_drop_mc_three.x = mySO.data.mytear_drop_mc_three_x;
tear_drop_mc_three.y = mySO.data.mytear_drop_mc_three_y;
CODE ADDED TO MAKE THE TIMELINE SAVE - - - - - - - - - -
// applied to the clickersave function
mySO.data.myBgFrame = 2;
mySO.flush ();
}
if (mySO.data.myBgFrame){
caseSwapper.gotoAndStop(mySO.data.myBgFrame);
}
Not sure I understood exactly what you mean, but if you use click on button1 for BG1, in the click function you could write:
mySO.myBgFrame = 1;
mySO.flush ();
and than, when you need to set the saved BG:
if (mySO.myBgFrame){
bgMovieClip.gotoAndStop(mySO.myBgFrame);
}
Is this what you need?

How can I make an object's key more dynamic?

I have an AssetManager singleton that I'm using as a data provider of sorts.
public static var L1_1:Object = {pic:'../Assets/images/test.jpg', answer:'someAnswer', pool: 'somePool'}
So I'm trying to use this in another class to get a path for an image class that takes a string as a param like so:
var image:Image = new Image(AssetManager.L1_1.pic , 540, 540);
This works great, however, I need to increment the second '1' above to make it dynamic so I can say for eg.,
var image:Image = new Image(AssetManager.L1_5.pic , 540, 540);
I have tried things like this, passing an imageNum property but can't sort it.
var tempObj:Object = ['AssetManager.L' + imageNum + '_' + imageNum + '.pic'];
trace(tempObj)
Is there an easier way to do this? Thanks!!
Static and public properties in AS3 can be accessed using bracket syntax.
Here's an example:
public static var L1_1:Object = {pic:'../Assets/images/test.jpg', answer:'someAnswer', pool: 'somePool'};
var imageNum:int = 1;
var propertyName:String = "L1"+ "_" + imageNum.toString();
var tempObj:Object = AssetManager[propertyName];
var pathToPic:String = tempObj.pic;
trace(pathToPic);
The above example should trace out ../Assets/images/test.jpg
However, be aware that bracket syntax is slower than dot syntax and is not type checked at compile time.
I hope this helps.
I have a fix for now by simply using an array like so:
public static var L1_1:Array = new Array();
L1_1[0] = ['../Assets/images/test.jpg', "apple", "appletakeout"];
And calling it with:
image = new Image(AssetManager.L1_1[0][imageNum], 540, 540);
And it works, just feels a tiny bit messy. If there's a more efficient way to do this I'd love to hear. Thanks.

Finding New and Updated Pages in EpiServer

I have a requirement to display lists of newly-created and updated pages in our Episerver intranet - say the last ten of each. I've tried using FindPagesWithCriteria but this returns no results. Here's the code I've tried:
PageDataCollection recentPages;
PropertyCriteriaCollection criteria;
PropertyCriteria upperBound;
PropertyCriteria lowerBound;
criteria = new PropertyCriteriaCollection();
upperBound = new PropertyCriteria();
upperBound.Condition = CompareCondition.LessThan;
upperBound.Type = PropertyDataType.Date;
upperBound.Value = DateTime.Today.ToString();
upperBound.Name = "Created"; // Or Saved for updated pages
criteria.Add(upperBound);
lowerBound = new PropertyCriteria();
lowerBound.Condition = CompareCondition.GreaterThan;
lowerBound.Type = PropertyDataType.Date;
lowerBound.Value = DateTime.Today.AddDays(-7).ToString();
lowerBound.Name = "Created";
criteria.Add(lowerBound);
recentPages = DataFactory.Instance.FindPagesWithCriteria(PageReference.StartPage, criteria);
I've also tried using the RecentlyChangedPagesFinder (as detailed here) - this returns some results, but when I try to use the set of results to build a PageCollection to databind into a PageList, again I get nothing output. And I can't see that I could use that for new pages, only updated ones.
The property name should be "PageCreated".
http://epiwiki.se/developing/properties/all-built-in-properties
You can also improve your FindPagesWithCriteria-syntax by going something like this:
var criterias = new PropertyCriteriaCollection
{
new PropertyCriteria()
{
Name = "SomeProp",
Type = PropertyDataType.PageType,
Value = "eh",
Condition = CompareCondition.Equal,
Required = true
},
new PropertyCriteria()
{
...
};
var pages = DataFactory.Instance.FindPagesWithCriteria(somePageLink, criterias);

Resources