/    Sign up×
Community /Pin to ProfileBookmark

Stuck on preloaded object problem

I’ve preloaded 12 images

[CODE]
function preload(imgObj,imgSrc) {
eval(imgObj+’ = new Image()’)
eval(imgObj+’.src = “‘+imgSrc+'”‘)
}
preload(‘img1′,’b1.gif’)
etc…..
[/CODE]

In a -for- loop I have

[CODE]
document.images[‘imgcon’+y+x].src = “img+rand(1,12)+.src”;
[/CODE]

this is seen as ‘img1.src’ instead of the object (or img2,img3 etc)

[CODE]
document.images[‘imgcon’+y+x].src = img+rand(1,12)+.src;
[/CODE]

I get –object expected

[CODE]
document.images[‘imgcon’+y+x].src = img1.src; (or img2,img3 etc)
[/CODE]

works

or

[CODE]
document.images[‘imgcon’+y+x].src = “b”+rand(1,12)+”.gif”
[/CODE]

works

So the 12 image objects exist but I can’t get the syntax right to get the code to add a random number after ‘img’ and not see it as a string.

Any Help??

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisMar 06.2008 — You would be better off to make an array.
<i>
</i>var imgs = new Array();
var which = 0;
function preload(imgObj, imgSrc) {
imgs[which] = new Image();
imgs[which].src = imgSrc;
which++;
}
preload('img1','b1.gif')
...
document.images['imgcon'+y+x].src = imgs[rand(1,12)].src;
Copy linkTweet thisAlerts:
@thedosmannauthorMar 07.2008 — That worked ---thanks--- I'm not stuck now. Sometimes you need a little bump.?
×

Success!

Help @thedosmann spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...