Home / Topic / Add external code

Add external code

Home Kwiksher Forums Help me! Add external code

Viewing 15 posts - 1 through 15 (of 15 total)
  • Jorge
    Participant
    #82511

    Hi,
    This is my very first attempt to add external code. The code is based on the Memory Game tutorial, with the necessary adaptations. This is the code:

    while #tList ~= 0 do
    local ind = math.random(#tList)
    table.insert(randList, tList[ind])
    table.remove(tList,ind)
    end

    local cards = { {164,188}, {604,188}, {1044,188}, {1485,188}, {164, 588}, {604,588},
    {1044,588}, {1485,588}, {164,988}, {604,988}, {1044,988}, {1485,988} }
    local card = {}
    for i = 1,#randList do
    card[i] = display.newImageRect( imgDir.. “mgC_card”..randList[i]..”.jpg”, 400, 360 );
    card[i].x = cards[i][1]
    card[i].y = cards[i][2]
    menuGroup:insert(card[i])
    end

    I gave it a name shuffleCardsChar and saved it as .txt Uhave used notepad for it)
    When I use the command Add External Code I give it the same name as above and point to a local directory where I plan to put all external codes.
    However, nothing happens. The field From file: remains empty.
    What am I doing wrong? The code?
    Thanks in advance.

    Administrator
    Keymaster
    #82512

    First of all this code is wrong. Check this tutorial on how to convert Kwik 2 projects to Kwik 3:http://kwiksher.com/tutorials_kwik/migrating-external-code/

    Also, there are different ways to add external code (you can have them in your main.lua file, in a page, in a button/action, etc). It seems to me the issue may be in the file name (make sure you see the name with the .txt or .lua extension in the Finder). Try also, whenever is possible to use only lower caps in external files names.

    Jorge
    Participant
    #82513

    Alex, many thanks for the Kwik answer.
    What I did:
    -changed the name as you have recommended
    -selected all the cards I have in the page and the “add external code”
    -changed the code (changes in bold)

    while #tList ~= 0 do
    local ind = math.random(#tList)
    table.insert(randList, tList[ind])
    table.remove(tList,ind)
    end

    local cards = { {164,188}, {604,188}, {1044,188}, {1485,188}, {164, 588}, {604,588},

    {1044,588}, {1485,588}, {164,988}, {604,988}, {1044,988}, {1485,988} }
    local card = {}
    for i = 1,#randList do
    card[i] = display.newImageRect( composer.imgDir.. “mgC_card”..randList[i]..”.jpg”, 400,

    360 );
    card[i].x = cards[i][1]
    card[i].y = cards[i][2]
    menuGroup:insert(card[i])
    end

    I am not sure if all card[i] must be layer.card[i]

    Now it was accepted, but i am not sure if everything is correct, which I would like to know before I move into the next step (buttons). Many thanks.

    Administrator
    Keymaster
    #82514

    In Kwik 3 there is no such thing as menuGroup anymore. Check the tutorial again to discover how to replace it. And NO, you should not add the “layer.” to the card part.

    Jorge
    Participant
    #82515

    Thanks, I have missed the scene one. If I succeed I will share the converted (to K3) code for the other Forum members benefit.

    Jorge
    Participant
    #82528

    Thanks for the hints Alex.
    I have managed to create the buttons to flip.
    My “only” problem now is that the images (the ones which we put directly into the Build folder) are not appearing underneath of the cards/buttons. I can see only and partly 3 images on the left side of the Simulator, of which two are the same;these images change as I refresh the Simulator as expected. These images are on Top of the cards/buttons.

    I hope this is to do with the following piece of code:
    ….
    table.insert(flipped,self) — updates the content of flipped table

    – if you have more than 8 cards, you must review this line here, as it was written to capture only the last character of the picture name.

    – For example, p1_photo1 will return “1″.
    if (randList[tonumber(string.sub(string.reverse(flipped[1].name),1,1))] == randList[tonumber(string.sub(string.reverse(flipped[2].name),1,1))]) then
    –equal images, move buttons to out of screen
    flipped[1].x = 5000
    flipped[2].x = 5000
    flips = 1 — returns the number of flips to 1
    flipped = {} –cleans the flipped table
    else
    ….

    I simply was not able to understand what/where I have to change the code for the number of cards (12).
    I understand that you are not sure if the code provided with the Tutorial works for more than 10 cards. I would like to see if it really works, since I believe I am not far away from succeeding, with your precious help, of course.

    For your reference and confirmation, I believe that there is a typing mistake in the code you have in the Tutorial
    flips = flips + 1 ; if flips > 2 then flips = 2 end
    It took me a while to figure it out and took the sign away and it worked well. Do you agree?
    Hope you can help and thanks once more.

    Administrator
    Keymaster
    #82529

    If you are learning how to code, I would suggest to stick to the tutorial (8 cards) in order to understand it first. The way this code was create (I am just re-checking it now), it supports only 8 cards. If you need more than that, you will need to rewrite this big condition:
    if (randList[tonumber(string.sub(string.reverse(flipped[1].name),1,1))] == randList[tonumber(string.sub(string.reverse(flipped[2].name),1,1))]) then

    If you are familiar with Lua code, you noticed that only the last digit (0-9) is being captured, reason why having a (for example) image10.png will not work (because only the “1” from the “10” will be captured).

    Lastly, I mentioned that you should not set the code to BEFORE LAYERS but I may be wrong because, as you described here, setting it to render AFTER layers, it will render the card images on top of the back cards (which is not right).

    Administrator
    Keymaster
    #82530

    By the way, check this post about more than 8 cards in your game: http://kwiksher.com/forums/topic/memory-game-more-8-card/

    Jorge
    Participant
    #82536

    Everything seems to be in line with the recommendations of the above mentioned post. The only thing i do not know is exactly where to put the following piece of code as mentioned in the same above post:

    if layer.cover1.x == 5000 and layer.cover2.x == 5000 and layer.cover3.x == 5000 and layer.cover4.x == 5000 and layer.cover5.x == 5000 and layer.cover6.x == 5000 and layer.cover7.x == 5000 and layer.cover8.x == 5000 and layer.cover9.x == 5000 and layer.cover10.x == 5000 and layer.cover11.x == 5000 and layer.cover12.x == 5000 then

    Images are now underneath the buttons.
    If I can not manage it in the next few hours I will have to do something else rather than to bother you further with it. If I knew that going to two digits Memo Game was so more complex I would have made a 8 one! Now that I have been that far I would not like to give up (yet).

    Thanks.

    Administrator
    Keymaster
    #82538

    Jorge, I wasn’t the one who wrote that posting so, I cannot help you much 🙁

    Jorge
    Participant
    #82553

    Alex, I have followed your advice and went back to 8 cover cards (for the time being) and changed the code, etc. accordingly.
    However, I still have the issue I had of the position of the images (the ones which we put manually into the Build folder) Those are not underneath the covers, but a lot to the left and up (I only see two), though they appear below the covers.
    I have checked the code as my understanding allows, rechecked the input concerning the position of the images in the shufflecards code, but can not resolve this issue.

    I am hoping this is a tiny thing which your very experience eyes will quickly spot (please see Lua code I am sending separately by email). Could you please be so kind and help.Thanks in advance.

    Jorge
    Participant
    #82556

    Alex, in the meantime I have noticed that the positioning of the covers (the images published through PS) in the Lua file are different from the position i see in the layer’s properties panel. In Lua the positions are related to the mid point of the image.
    I have changed the positions of the cards (the ones in the Build file) in shufflecards code accordingly. What happens is that I would see 2 images on the very left side of the board. It seems that the y position is now ok, but the x not.

    Jorge
    Participant
    #82557

    By changing the position of the images based on the Lua file and not on the PS Layers Property, made in fact all the images and buttons being now aligned.
    Furthermore, something was missing in the code.
    The 8 cards MG works fine now. Thanks for the support.

    • This reply was modified 5 years, 5 months ago by  Jorge.
    Administrator
    Keymaster
    #82568

    Jorge,

    I believe your project has images with sizes, and consequently X and Y positions different than my original project (my Lua table has the right position to cards originally designed in my project). I just recreated the entire project, changing only the needed things to make the project compatible with Kwik3, and everything worked fine.

    Jorge
    Participant
    #82570

    Alex,
    In fact the dimension of the images are different.
    I have used your code, adjusted it accordingly and everything works fine.
    However, I still had to use the Lua positions and not the PS positions.
    Hope this will not lead to trouble, since in the Simulator it works fine.
    Many thanks for being so helpful.

    • This reply was modified 5 years, 5 months ago by  Jorge.
Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.

Privacy Preference Center

Strictly Necessary

these coolies are for WP-SpamShield, gdpr-wp, woocommerce, paypal

PHPSESSID, SJECT16, JCS_INENREF, JCS_INENTIM, gdpr, woocommerce_cart_hash, woocommerce_items_in_cart, woocommerce_recently_viewed, wordpress_, wordpress_logged_in_, wordpress_sec_, wordpress_test_cookie, wp_woocommerce_session_, AKDC, akavpau_ppsd

Functionality

WordPress cookie created when auto-saving a post in the editor.

wp-saving-post, wp-settings-, wp-settings-time-

Performance

google analytic

__utma,__utmb,__utmc,__utmz,_ga,_gid,_gat

Advertising