It looks like you're new here. If you want to get involved, click one of these buttons!
Bat1 = class()
function Bat1:init()
-- you can accept and set parameters here
self.x = 0
self.y = HEIGHT/2
end
function Bat1:draw()
-- Codea does not automatically call this method
self.x=math.min(self.x,WIDTH/2-60)
sprite("Planet Cute:Wood Block",self.x,self.y,80)
end
function Bat1:touched(touch)
-- Codea does not automatically call this method
self.x=math.min(touch.x,WIDTH/2-60)
self.y=touch.y
end
Bat2 = class()
function Bat2:init()
-- you can accept and set parameters here
self.x = WIDTH
self.y=HEIGHT/2
end
function Bat2:draw()
-- Codea does not automatically call this method
self.x=math.max(self.x,WIDTH/2+60)
sprite("Planet Cute:Wood Block",self.x,self.y,80)
end
function Bat2:touched(touch)
-- Codea does not automatically call this method
self.x=math.max(touch.x,WIDTH/2+60)
self.y=touch.y
end
--main
function setup()
print("hello world")
displayMode(FULLSCREEN)
bat1=Bat1()
bat2=Bat2()
end
function draw()
background(0, 0, 0, 255)
ellipse(WIDTH/2,HEIGHT/2,200)
strokeWidth(10)
stroke(255, 255, 255, 255)
fill(218, 15, 15, 255)
rect(0,HEIGHT/2-HEIGHT/16,10,HEIGHT/8)
rect(0.98*WIDTH,HEIGHT/2-HEIGHT/16,10,HEIGHT/8)
line(WIDTH/2,0,WIDTH/2,HEIGHT)
bat1:draw()
bat2:draw()
end
function touched(touch)
bat1:touched(touch)
bat2:touched(touch)
end
I find that two bats share the same touch.x,I want to know how to debug,i want to them move without effecting each other.
I wonder if anyone can help me, Thanks a lot!
Comments
@wenhaozheng When you create a class, don’t seperate everything. Create a bat class then have one bat:init, bat:draw, and bat:touched. Your bat1, bat2 code in setup, draw, and touched look OK.
@dave1707 thanks,but these bats need to move in different limited area(bat1 left screen,bat2 left screen),I can't find a way to limit them in one class.
@wenhaozheng Here’s an example. Touch each block to move them.
PS. I added code to limit each block to its own area.
@wenhaozheng - have you checked out the example project ping. It seems to do most of what you need?
@wenhaozheng - do your bats move in one dimension or in two? What I mean is do they only move sideways along the say X axis, or do they move in both X and Y in their half of the screen?
@dave1707 wow! Thank you for taking the trouble to help me.! I do appreciate it!
It’s really helpful!
@wenhaozheng That’s why were here, to help. If you have other questions, there are a lot of people here with answers.
@Bri_G both x and y,
@dave1707
I’ve changed some of your script to limit the two bats’ position ,but now,i find that these two bats can’t move in correct state,they can’t be limited neither.I’m really sorry to bother you but,I can’t see another way..
@wenhaozheng - you need to expand your bat class to limit movement of the bat.
See below:
I’ll leave it to you to figure out the limits nx,fx,ly,uy represent near x, far x, lower y and upper y.
When you post code could you ensure you use ~~~ before and after the code so that it is formatted correctly.
@Bri_G thanks,I will spend more time to learn from your script.
@wenhaozheng - the code I posted only restricts the bats to their areas. You still need to consider ID and more than one touch at once. If you move one of the paddles and cross the halfway line you automatically pick up the other paddle. Also if you touch outside the bat the bat appears under your finger.
Ponder over that and post again if you have problems.
Right,I’m working on it !thanks a lot
@wenhaozheng I modified my code above to limit each block to its own area.
@dave1707 - I will post a photo of the error, it is in the ball tab of ping. Just playing around with it. Error in this routine but OK if it’s in setup(). My code below, trying to put a ball in with text() and emojis.
Put whatever emoji in the text() call. There may be other clashes - could be just related to textual formatting versus that of text.
@Bri_G I didn’t get an error when I ran it. Here’s what I had in a seperate project. I added the Ball:init function. Don’t know if that matches yours. This is on version 130.
PS. I then added your function to Ping and didn’t have any problem with it there either. I added an emoji in the text line, I don’t think it matters which emoji.
PSS. I tried it on my iPad Pro also without an error.
@dave1707 I’ve solved the touch problem,now,I find another one,my ball always shakes when it’s nearby the bats and the screen padding,I use some of script in examples,but It can’t work well in my project
I’m so sorry that I’m a starter and not very good at English,so I wonder if you can figure out the wrong point in my script.And I really don’t know which class is the wrong point in.So,please forgive me to copy all my script again.That would be very thankful!
--# Vvv
It often shakes around the corners or on the bats’ frame
And the game seems “die”,I mean none of the bats can moving
@wenhaozheng I suggest you look over the Ping example. That example has one class for the paddles and one for the ball. You’re code has one class for the ball but 2 classes for the paddles. Trying to take code from ping and make it work in your code isn’t going to work because of the 2 paddle classes.