How do I load JSON file on the basis of Array Indices? [URGENT]

0 favourites
  • 13 posts
From the Asset Store
Source code, art assets and music tracks to remake this game
  • Hey guys!

    I am stuck at a problem, it sounds complicated at least for me. So I will try my best to explain.

    I have a list of an array with IMG urls. And on my home page I have set for loop to create sprite objects with those images randomly.

    Now What I want is, whenever those images are clicked on Home page, I want to load JSON file related to that image.

    For example:

    On the home page, I have 2 images from Quizlist array at X0,Y1 and X0, Y2 axises.

    When Image One clicked I want to load a JSON file named "Quiz1" and when 2nd image clicked, I want to load "Quiz2"

    I tried loading via ajax but it doesnt allow us to add custom expressions.

    BTW, The Images that gets loaded on Home Page will be random, so I want to load corresponding JSON files when those images clicked.

    I hope I have explained correctly if you still need more info to help me, please feel free to ask.

    This is urgent, my work has been stopped just because of this one thing.

    This is the image of my event sheet right now!

    Thanks in advance <3

    WackyToaster

    Ashley

  • Instead of "AJAX request project file" use "AJAX request URL", where URL is the file name. So you can change your code like this:

    Touch On Tap gesture on Sprite 
    	Sprite animation frame=0 : AJAX request URL "quiz0.json", tag "quiz"
    
    	else
    	Sprite animation frame=1 : AJAX request URL "quiz1.json", tag "quiz"
    
    	else
    	Sprite animation frame=2 : AJAX request URL "quiz2.json", tag "quiz"
    
    

    Instead of animation frame you can use some other condition, or even an instance variable on the Sprite containing the file name string.

  • dop2000 Hey, actually i wanted this in automated way, means I just want to add URLS to the Array and the all Sprites should get updated automatically. There is a video on youtube that guy does the same. Please let me know how to do like that.

    Link: youtu.be/uO0sIr9rNpc

  • When you create random images for different quizzes on the layout, you can also set a corresponding file name with quiz questions for each sprite (add it to an instance variable "quiz_file_name"). Then on tap you can simply request Sprite.quiz_file_name with AJAX.

    I'm not watching a 24-minute video of some guy typing in Notepad, sorry :)

    Please post your project.

  • Thank u :)

    but should i use For loop or For Each to create multiple sprite with different images?

  • Yes, probably. How are you creating them now?

  • I am using For loop now. It is creating Spirtes but they are not getting loaded with images. Please check my event sheets.

    First Layout:

  • 2nd LAYOUT:

  • 1st layout - why do you loop to array.height? I don't know your array structure, but it's usually array.width. Also, when you are loading multiple images into one sprite, you need to load them to different frames. Otherwise all sprite instances will have the same image. So you create a sprite, change its frame, and then load image.

    2nd layout - after you switch to another layout, "hm" sprite is no longer available. Even if you set it as Global, this layout doesn't know which sprite was tapped. So before changing layouts you need to save hm.Qname in a global variable and then on start of the second layout use that variable to request a file with AJAX.

    .

    You need to start using debugging tools. Run the game in Debug Mode, you will be able to see the array content, how many hm sprites are created, instance variables for these sprites etc.

    Also, use "Browser Log" to output debug messages. For example, after "AJAX request hm.Qname" add another action:

    Browser Log "Requesting filename: " & hm.Qname

    Then run the project, press F12, open Console and see what happens.

  • thank you dop2000. It worked but the only problem right now is I am not able to display image randomly from array on 2nd layout.

    My Array looks like this, I wanna display random images from first row.

    i am using this command now "Array.At(floor(random(0, Array.Width)))" but still not working.

  • Have you tried "Browser Log Array.At(floor(random(0, Array.Width)))" ? You'll see if this expression works or not.

    If you see the correct URL in console log, then the issue is with something else.

  • thanks again its working fine :) dop2000

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • hey dop2000

    Can u answer my other question here as well? construct.net/en/forum/construct-3/how-do-i-8/load-multiple-spirites-one-143456

    Thanks in advance!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)