Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

I need some serious help
Started by ntomanelli90 May 13 2018 04:27 PM

8 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ntomanelli90

[GLOBAL: userInfoPane.html]
ntomanelli90
  • Observer

  • 5 posts
  • Corona SDK

Hello there,

 

I may have bitten off more than I can chew with this one. Here's the background, I'm an education major with close to no experience with programming taking an online elective course that basically involved following the getting started guide. I now have to create an educationally based game as my final assignment and although I completed the 8 chapter tutorials; I feel like I don't know enough to accomplish this. 

 

I've spent the whole weekend behind my computer attempting to make a simple game that I can submit as my final project. Here is what I am trying to do;

 

I took similar mechanics from the star explorer game and instead used a dart and three targets that will sit at the top of the screen. The goal is to have a game that places multiplication problems somewhere on the lower portion of the screen while three possible answers will be displayed above or on the three targets at the top. The player will then need to choose the correct target to hit and tap the dart to fire at one of the targets. 

 

As it stands I have all images on the screen and the dart is able to fire up to the top. I have been searching all weekend with no success to find out how I can add in multiplication problems from #'s 1-10, and the answers that correlate with specific targets. I also need a collision event to occur when the dart hits a target because right now it just passes it by and flies off screen. Please if there is anyone out there that can give me some advice you would be my hero.

 

Here is a copy of the code I currently have in the main.lua

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

-- housekeeping stuff

display.setStatusBar(display.HiddenStatusBar)

local physics = require( "physics" )
physics.start()
physics.setGravity( 0, 0 )

-- Initialize variables
local background
local dart
local title
local text
local target
local score = 0
local wrong = false

-- Set up display groups
local backGroup = display.newGroup()  -- Display group for the background image
local mainGroup = display.newGroup()  -- Display group for the dart.
local uiGroup = display.newGroup()    -- Display group for UI objects like the score

local background = display.newImageRect( backGroup, "bg.jpg", 620, 690 )
background.x = display.contentCenterX
background.y = display.contentCenterY

local dart = display.newImageRect( mainGroup, "dart.png", 37.5, 112.5 )
dart.x = display.contentCenterX
dart.y = display.contentHeight-25

local title = display.newImageRect( backGroup, "title.png", 300, 100)
title.x = display.contentCenterX
title.y = display.contentHeight-325

local function createTarget()

    local newTarget = display.newImageRect( mainGroup, "target.png", 85, 85 )
    target.x = display.contentWidth-265
    target.y = display.contentHeight-420

end
local target = display.newImageRect( mainGroup, "target.png", 85, 85 )
target.x = display.contentWidth-265
target.y = display.contentHeight-420

local target = display.newImageRect( mainGroup, "target2.png", 85, 85 )
target.x = display.contentWidth-159
target.y = display.contentHeight-420

local target = display.newImageRect( mainGroup, "target3.png", 85, 85 )
target.x = display.contentWidth-55
target.y = display.contentHeight-420


local function throwDart()

    local newDart = display.newImageRect( mainGroup, "dart.png", 37.5, 112.5 )
    physics.addBody( newDart, "dynamic", { isSensor=true } )
    newDart.isBullet = true
    newDart.myName = "Dart"

    newDart.x = dart.x
    newDart.y = dart.y
    newDart:toBack()

    transition.to( newDart, { y=-150, time=500, } )
        onComplete = function() display.remove( dart ) end
end

dart:addEventListener( "tap", throwDart )


local function dragDart( event )

    local dart = event.target
    local phase = event.phase

        if ( "began" == phase ) then
            -- Set touch focus on the dart
            display.currentStage:setFocus( dart )
            -- Store initial offset position
            dart.touchOffsetX = event.x - dart.x

        elseif ( "moved" == phase ) then
            -- Move the dart to the new touch position
            dart.x = event.x - dart.touchOffsetX

         elseif ( "ended" == phase or "cancelled" == phase ) then
            -- Release touch focus on the dart
            display.currentStage:setFocus( nil )
         end

         return true  -- Prevents touch propagation to underlying objects
end

local function onCollision( event )

    if ( event.phase == "began" ) then

        local obj1 = event.object1
        local obj2 = event.object2

        if ( ( obj1.myName == "dart" and obj2.myName == "target" ) or
             ( obj1.myName == "target" and obj2.myName == "dart" ) )
        then
        -- Remove both the dart and target
            display.remove( obj1 )
            display.remove( obj2 )
        end
    end
end

dart:addEventListener( "touch", dragDart )

I tried starting a function to create each target but I don't think I have enough knowledge and whatever I've watched or read over the past two days hasn't helped me understand

 

here is a pic for reference OHwHH4.png



[TOPIC: post.html]
#2

remiduchalard

[GLOBAL: userInfoPane.html]
remiduchalard
  • Contributor

  • 301 posts
  • Corona SDK

Hello,

 

You have a lot of problems your code. I am too lazy to read you first paragraph, do you already know how to code in any language?

I am asking you that because you define 4 times the same value (target). If you want to have many target, they need to have a different name or to be in an array.

ex:

local target={}
target[1]=display...
target[2]=display..

Lua is one of the most simplest language to learn. Spend one hour to understand it and you will be able to code whatever you want



[TOPIC: post.html]
#3

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 393 posts
  • Corona SDK

Hi,

 

Every object needs to have a unique name.  If you name everything "target", each new target will overwrite the previous target.  Use names like "target1", "target2", "target3".

 

 1. As you are using transitions to move objects around the screen, you can simplify your code by removing physics objects. In fact, you don't need physics, collisions or dragDart()

 

2.  Look into math.random( ) to make your multiplication problems 

   
local function presentProblem(  )
    local num1 = math.random( 10 ) -- generate a random number from 1 to 10
    local num2 = math.random( 10 ) -- generate a random number from 1 to 10
    local correctAnswer = num1 * num2
    local falseAnswer1 = math.random( 10 ) * math.random( 10 )
    local falseAnswer2 = math.random( 10 ) * math.random( 10 )
end

 

Use display.newText to present num1 and num2 on screen to the user

your 3 answers are: correctAnswer, falseAnswer1 and falseAnswer2

 

3. You don't need collision detection.  Use the onComplete parameter in your transition.to( ) method to tell you when the transition has completed.

 

4. You don't need to drag the dart.  Just have the user tap on the target and use the targets x and y position to set the transition for the dart.

 

look on the marketplace for sample projects.  Some are free, come cost a few bucks.

 

Get back to us with your changes and we'll try to help some more.


  • roaminggamer likes this

[TOPIC: post.html]
#4

ntomanelli90

[GLOBAL: userInfoPane.html]
ntomanelli90
  • Observer

  • 5 posts
  • Corona SDK

I still don't really understand how to display the presentProblem function. I've tried display.newText but I get an error when trying to implement num1 or num 2. I tried placing those variables into quotes and that just caused it to say "num1" on the screen. 



[TOPIC: post.html]
#5

remiduchalard

[GLOBAL: userInfoPane.html]
remiduchalard
  • Contributor

  • 301 posts
  • Corona SDK

Your base is wrong

local target = display... -- will display the object and you can edit the object via target value
target = display... -- will display a new object and you can edit the object via target value but the previous target doesn't exist any more
local target=...
local target=... -- it's not possible to define two times the same value local in the same {}. Lua isn't going to tell you to **** off because you write something impossible

You need to know how to work value, function and array before go further



[TOPIC: post.html]
#6

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,491 posts
  • Corona SDK

It does show that these tutorials are great for explaining how things work when everything is done perfectly, but are useless for teaching what to do when things go wrong - how to read error messages, debug code, strip things back to the last known working point etc.



[TOPIC: post.html]
#7

remiduchalard

[GLOBAL: userInfoPane.html]
remiduchalard
  • Contributor

  • 301 posts
  • Corona SDK

Corona use lua to work. Lua is the language and corona the way to display things on every device.

Before learn corona you need to learn Lua or another langage as C,C++,C#,pyhton,php,java....

I haven't seen your tutorial. It's like you apply for a course if you haven't prerequisite. It's like began to do geometry before learn base of math. Spend one or two hour to learn lua and you will understand without any difficulties corona

For example when I began to work with corona I didn't know lua but it wasn't a problem because I have already code in an other language.

Lua is very simple that's why it's easy to switch from any language to lua.



[TOPIC: post.html]
#8

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 1,840 posts
  • Corona SDK

It's such a shame the forums are full of "I'm doing this for school and it's not working".... sad times for sure



[TOPIC: post.html]
#9

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 393 posts
  • Corona SDK

@ntomanelli

When is your project due?




[topic_controls]
[/topic_controls]