object list and stacks...

Monkey Forums/Monkey Beginners/object list and stacks...

dubbsta(Posted 2016) [#1]
trying to learn to manipulate objects with lists and stacks. so far this is what i did, it compiles but doesn't render the image.

Class coins 

 Field x:Int,y:Int
 Field speed:Int =1


   Method New(x:Int,y:Int )
	    
	 	Self.x = x
	 	Self.y = y
	 	 
   End 
   
    Method draw()
      DrawImage(image,x,y)
    End 


End 





    
Class test2 Extends App

 Field maxCoins:Int = 10
 Field Coin:coins
 Field image:Image
 Field coinDraw:List<coins>= New List<coins>()
    
   
    
		  Method OnCreate()
		  
		   		SetUpdateRate(60)
		   		
		   		image = LoadImage("Coin_Front.png",52,52)
		   		Coin = New coins
		  End 
		  
		  
		  
		  
		  
		  Method OnUpdate()
		 
			 
		  End 
		  
		  
		  
		  
		  
		  
		  Method OnRender()
		  
		  	Cls (0,0,0)
		  	 coinDraw.AddLast(New coins(150,150))
		  	 
		  End 
		    
End  
   
   
   
   
   Function Main()
   
  	 New test2()

     
   End  



Pakz(Posted 2016) [#2]
Addlast is used to add an item to the list. It does not draw anything.

What you need to do is call the draw method for each element in the list.

to loop through the list and use the draw method do :

This should be in the render named with your list name.
for local i:=eachin mylist
 i.draw()
next


edit :
Import mojo

Class coins 

	Field x:Int,y:Int
 	Field speed:Int =1
	Method New(x:Int,y:Int )	    
	 	Self.x = x
	 	Self.y = y	 	 
   End    
    Method draw()
      DrawRect(x,y,10,10)
    End 
End 
    
Class test2 Extends App

 Field maxCoins:Int = 10
 Field Coin:coins
 Field coinDraw:List<coins>= New List<coins>()
        
		  Method OnCreate()		  
		   		SetUpdateRate(60)		   		
		   		Coin = New coins
		   		coinDraw.AddLast(New coins(150,150))
		  End 
		  		  
		  Method OnUpdate()
		 			 
		  End 
		  
		  Method OnRender() 
		  	Cls (0,0,0)
		  	For Local i:=Eachin coinDraw
		  		i.draw
		  	Next		  	 
		  End 		    
End  
      
Function Main()
   New test2()
End



Gerry Quinn(Posted 2016) [#3]
Also, in the original version, image is a field of test2, not of coins.

You need to arrange for the coins.draw() method to have access to the single instance of test2, or to its image field. [There are numerous ways in which this can be done.]


dubbsta(Posted 2016) [#4]
ahh i originally had it in the coins class and thought it was wrong...ok thanks guys will give it a try.


Shinkiro1(Posted 2016) [#5]
Also, in OnCreate() the line
Coin = New coins

just creates an instance, then is never used.

I tried to rename variables and classes to make it a bit clearer:
Class Coin 'it's a blueprint for just 1 coin
	Field x:Int,y:Int
 	Field speed:Int = 1
	
	Method New(x:Int,y:Int)
	 	Self.x = x
	 	Self.y = y
	End    
	
    Method draw()
		DrawRect(x,y,10,10)
    End
End 
    
Class test2 Extends App
	Field maxCoins:Int = 10
	Field coinList:List<Coin>= New List<Coin>
        
	Method OnCreate()		  
		SetUpdateRate(60)		   		
		
		'Add 1 coin
		Local coin:Coin = New Coin(150, 150)
		coinList.AddLast(coin)
	End 
  
	Method OnRender() 
		Cls (0,0,0)
		For Local coin:= Eachin coinList
			coin.Draw()
		Next		  	 
	End   
End  
      
Function Main()
	New test2()
End


btw: You should always use Strict mode as the compiler will catch more errors when compiling.
Just start every monkey file with
Strict



Gerry Quinn(Posted 2016) [#6]
You don't want it as an ordinary field in the coins class either, or at least you don't want to call LoadImage() multiple times. (Having an image field in each class instance that points to a single global image is okay, and even ideal if there are several different types of coins, but you don't want each coin to hold a complete image.)

So you will probably have a global image in coins, but you will initialise it just once from the test2.OnCreate() method.

You can start by doing as Pakz did, and dodge the issue by dropping the Image and using DrawRect() instead. This stuff isn't complicated when you understand it, but it's best not to try to deal with too many unfamiliar issues at once. Once you have that working, you can try to use an image instead.


dubbsta(Posted 2016) [#7]
i get this for the most part, now i cant figure out how to create more boxes at the end of the previous one.
i tried a for loop in the oncreate then in the onupdate with the coin.addLast but not getting it. can someone provide me an example to study? i just want to draw more boxes with the same code...this is driving me nuts lol


Pakz(Posted 2016) [#8]
You mean you want more then one coin? Below here is a example I have on my github and blog.

https://github.com/Pakz001/MonkeyXExamples/blob/master/Monkey-X%20-%20Getting%20started%20-%20Class%20in%20List%20-%20Updating%20and%20Drawing%20-%20example.monkey

Here is a link to my example blog thread. Lots of examples named and sorted in difficulties :
http://www.monkey-x.com/Community/posts.php?topic=9290&page=1


dubbsta(Posted 2016) [#9]
ty sir this is just what im looking for will study now!


Simonsuuri(Posted 2016) [#10]
if also noticed that you didnt have "image" variable in coins class so it doesnt have anything to draw with





dubbsta(Posted 2016) [#11]
Yeah had it there before I posted but took it out, I get pakz code but still having trouble loading image. Will take a look at your code later thx!


dubbsta(Posted 2016) [#12]
finally got it!!! but.......
scroll method wont work when i call it. i call it with the right prefix in OnUpdate but does nothing but i can use for ex. i.x but not i.scroll(1)

Class bg '320x180 

  Field x:Int
  Field y:Int
  Field speed:Int
  Field bg1:Image
  Field  name:String
  
                 
		Method New(x:Int,y:Int,bg1:Image)
		  Self.x = x
		  Self.y = y 
		  Self.bg1 = bg1
		End 
		
            
		Method scroll(Speed:Int)   'why does this not work when i call it 
		  Self.speed = speed 
		   
		   x = x + speed
		   
		End 
		
		Method create(name:String)
		Self.name = name
		  bg1 = LoadImage("")
		  
		End   
		  
		Method Draw()
		
		  DrawImage (bg1, x,y)
		
		End
		 
End Class
  



Pakz(Posted 2016) [#13]
In the code above you have a upper case S and a lower case s with speed. They need to be precisely the same.

Speed <> speed.


dubbsta(Posted 2016) [#14]
wow didnt notice that thanks sheesh...! all working, working on my naming conventions now lol