使用其他设备时自动更改对象的位置 [英] Automatically change the position of objects when using different devices

查看:77
本文介绍了使用其他设备时自动更改对象的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对lua和corona还是陌生的(准确地说是3天),并且我正在尝试使用多种设备在屏幕上显示对象.据我了解,要使对象自动重新定位,我需要获取屏幕尺寸并对其进行一些计算,以进行必要的调整.这是我要执行的操作的一个示例:

I am still new to lua and corona (3 days to be exact) and I'm trying to my objects on the screen using multiple devices. From what I understand, to make the objects automatically reposition themselves, I need to get the screen size and do some calculation to it to make the necessary adjustment. Here's a sample of what I'm trying to do:

obj1 = display.contentWidth*0.50

这将把我的对象放到屏幕中间,但是在某些情况下这似乎无效,所以我的问题是,自动重定位对象的更好方法是什么?尤其是不再位于屏幕中央的对象.

This will then put my object to the middle of the screen but there are instances wherein this doesn't seem effective so my question is, what is a better approach to automatically reposition my objects? Especially objects that are no longer in the center of the screen.

推荐答案

我用不同的方式来做.我认为这可能对您有帮助.我认为,只需为宽度和高度创建乘数值(根据您在其中编码的模拟器),然后将每个宽度或高度参数与此值(如下所示)相乘即可:

I'm doing it in a different way. I think this may help you. As my opinion, just create multiplier values for width and height(according to which simulator you are coding in), and multiply your each width or height parameter with this(as below):

--------------------------------------------------------------------------
       -- choosing xMultiplier and yMultiplier values --
--------------------------------------------------------------------------
  local xMultiplier = display.contentWidth/320  
  local yMultiplier = display.contentHeight/480
     --[[ I used 320 here because i'm using iPhone Simulator 
         (320 is the width of the simulator you are coding in)
          I used 480 here because i'm using iPhone Simulator 
          (480 is the height of the simulator you are coding in)--]]


--------------------------------------------------------------------------
       -- creating background and positioning it --
--------------------------------------------------------------------------
  local bg = display.newImageRect("bg.png",320*xMultiplier,480*yMultiplier)
  bg.x = 160*xMultiplier ; bg.y = 240*yMultiplier

--------------------------------------------------------------------------
       -- creating object and positioning it -- 
--------------------------------------------------------------------------
  local rect = display.newImageRect(0,0,50*xMultiplier,50*yMultiplier)
  rect.x = 160*xMultiplier ; rect.y = 100*yMultiplier

--------------------------------------------------------------------------

注意:如果您在项目中使用config.lua文件,则此方法可能无效.

Note: If you are using config.lua file in your project, this may not work.

专业人士:这只需要一张图片.

Pros: This needs only one image.

缺点:它可能会影响高分辨率设备中图像的清晰度.因此,请选择具有适当分辨率的图像.

Cons: It may affect the clarity of images in devices with high resolution. So choose an image with suitable resolution.

继续编码...:)

这篇关于使用其他设备时自动更改对象的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆