Fire Dragon Posted February 5, 2006 Share Posted February 5, 2006 Well,I tried create my first object,what should draw image,write text etc.However,I got problem with image.I can't get it work.I tried put image's information into variable,but this idea crashed.How you recommend to put image or any other feature in object?Help is apreciated.Thanks. Link to comment Share on other sites More sharing options...
Chocolate570 Posted February 5, 2006 Share Posted February 5, 2006 I don't understand. Will this be used for your game, or what? This is all I can think of. wizard = new Object();wizard.src="src of image"wizard.writer="what writes"wizard.txt="text"wizard.imager="imager" Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 6, 2006 Author Share Posted February 6, 2006 I don't understand. Will this be used for your game, or what? This is all I can think of.wizard = new Object();wizard.src="src of image"wizard.writer="what writes"wizard.txt="text"wizard.imager="imager" <{POST_SNAPBACK}> No,this only for my learning.I'm beginner with object programming,so I wanted ask this,because I didn't found way to do it.I created this kind test object based your example,but I can't get picture to show up.<script type="text/javascript">function wizard(src,writer,txt,imager){this.src=src;this.writer=writer;this.txt=txt;this.imager=imager;}blue=new wizard();blue.src="lotad.gif"blue.writer="Blaaaaaa!"blue.txt="text"blue.imager="imager"</script> What is wrong,huh?I can't get image to my object,no matter what I try Link to comment Share on other sites More sharing options...
Chocolate570 Posted February 6, 2006 Share Posted February 6, 2006 <script type="text/javascript">function wizard(src,writer,txt,imager){this.src=src;this.writer=writer;this.txt=txt;this.imager=imager;}blue=new wizard();blue.src="lotad.gif"blue.writer="Blaaaaaa!"blue.txt="text"blue.imager="imager"</script> Well, you can't make an object from another function. The object has to be declared as: blue=new Object(). It should work once you've fixed the bug I stated above, but your wizard() function wont, so I suggest you add an extra parameter in the function and then call it, like this: <script type="text/javascript">function wizard(src,writer,txt,imager){document.getElementById(this).src=src;document.getElementById(this).writer=writer;document.getElementById(this).txt=txt;document.getElementById(this).imager=imager;x=document.createElement("img");x.src=this.src;}blue=new Object();blue.src="lotad.gif"blue.writer="Blaaaaaa!"blue.txt="text"blue.imager="imager"</script> What that should do is make the object's values worth different things and then make an image tag and change the src of it to the object's SRC. Sorry if it doesn't work, I'll have another go Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 6, 2006 Author Share Posted February 6, 2006 What that should do is make the object's values worth different things and then make an image tag and change the src of it to the object's SRC.I'm completely out now What you mean about this?I learned object programming from one book,and it tell object's creation completely different way Book shows that first you must create class with "this" clips,and after it you can create object.That's why I added thatthis.src=src;this.writer=writer;this.txt=txt;this.imager=imager code.Do you mean,that I must first create image <img src="lotad.gif">then get it someway "inside" my object?Or what? Link to comment Share on other sites More sharing options...
aspnetguy Posted February 6, 2006 Share Posted February 6, 2006 This is how I declare my objects. function Player(type) { var newPlayer = new Image(); newPlayer.src = (type=='pacman')? pacmanImage : ghostImage; newPlayer.width = playerSize; newPlayer.height = playerSize; newPlayer.style.position = 'relative'; newPlayer.style.top = (type=='pacman')? '0px' : (boardHeight-((playerSize)+borderWidth)) + 'px'; newPlayer.style.left = (type=='pacman')? '0px' : (boardWidth-((playerSize*2)+borderWidth)) + 'px'; newPlayer.setAttribute("id",(type=='pacman')? 'playerPacman' : 'playerGhost'); gameBoard.appendChild(newPlayer); this.params = new Object(); this.move = function(){playerMove(newPlayer)}; } This is my Player object.based on the type of Player it creates an image for the player and write it to the board and defines a .move method which is tied to the playerMove function. Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 6, 2006 Author Share Posted February 6, 2006 Aspnetguy's code was little complicate,but if understood right,you can store image data in variable,and then call it for object using something like this code: var sprite_image=<img src="sprite.gif">object1.sprite=sprite_image; Something like this,eh?However,I not be sure about that image code in variable. Link to comment Share on other sites More sharing options...
aspnetguy Posted February 6, 2006 Share Posted February 6, 2006 Aspnetguy's code was little complicate,but if understood right,you can store image data in variable,and then call it for object using something like this code:var sprite_image=<img src="sprite.gif">object1.sprite=sprite_image; Something like this,eh?However,I not be sure about that image code in variable. <{POST_SNAPBACK}> it would be more like this..sprite would be the objectfunction Sprite(imgsrc){ this.params = new Object(); this.src = imgsrc;} you could then call this object like so var mySprite = new Sprite('sprite.gif'); Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 7, 2006 Author Share Posted February 7, 2006 So if I understood it right,this clip creates new object,what should have image: new Sprite('sprite.gif'); However,it still won't work.With variable,or without variable.What I made wrong?I tried call object,but it won't work.Sometimes I feel,that I never learn OOP... Link to comment Share on other sites More sharing options...
aspnetguy Posted February 8, 2006 Share Posted February 8, 2006 I had trouble getting it to work too. I rewrote it to this: <html><head> <title>test</title><script> var mySprite = new Sprite('sprite.gif'); function Sprite(imgsrc) { var newSprite = '<img src="'+imgsrc+'" alt="" id="newSprite"/>'; this.params = new Object(); this.obj = newSprite; document.write(newSprite); }</script></head><body></body></html> Link to comment Share on other sites More sharing options...
Fire Dragon Posted February 8, 2006 Author Share Posted February 8, 2006 Thank you ASpnetguy!That helps my studies very much! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now