Matlab GUI: How to Save the Results of Functions (states of application) - user-interface

I would like to create an animation which enables the user to go backward and forward through the steps of simulation.
An animation has to simulate the iterative process of channel decoding (a receiver receives a block of bits, performs an operation and then checks if the block corresponds to parity rules. If the block doesn't correspond the operation is performed again and the process finally ends when the code corresponds to a given rules).
I have written the functions which perform the decoding process and return a m x n x i matrix where m x n is the block of data and i is the iteration index. So if it takes 3 iterations to decode the data the function returns a m x n x 3 matrix with each step is stired.
In the GUI (.fig file) I put a "decode" button which runs the method for decoding and there are buttons "back" and "forward" which have to enable the user to switch between the data of recorded steps.
I have stored the "decodedData" matrix and currentStep value as a global variable so by clicking "forward" and "next" buttons the indices have to change and point to appropriate step states.
When I tried to debug the application the method returned the decoded data but when I tried to click "back" and "next" the decoded data appeared not to be declared.
Does anyone know how is it possible to access (or store) the results of the functions in order to enable the described logic which I want to implement in Matlab GUI?

Ultimately, this is a scoping of variables problem.
Global variables is rarely the right answer.
This video discusses the handles structure in GUIDE:
http://blogs.mathworks.com/videos/2008/04/17/advanced-matlab-handles-and-other-inputs-to-guide-callbacks/
This video discusses sharing of variables between GUIs and could apply to a single GUI problem also.
http://blogs.mathworks.com/videos/2005/10/03/guide-video-part-two/

The trick is to use nested functions so that they share the same workspace. Since I already started with an example in your last question, now I'm simply adding GUI controls to enable going forward/backward interactively, in addition to play/stop the animation:
function testAnimationGUI()
%# coordinates
t = (0:.01:2*pi)'; %# 'fix SO syntax highlight
D = [cos(t) -sin(t)];
%# setup a figure and axis
hFig = figure('Backingstore','off', 'DoubleBuffer','on');
hAx = axes('Parent',hFig, 'XLim',[-1 1], 'YLim',[-1 1], ...
'Drawmode','fast', 'NextPlot','add');
axis(hAx, 'off','square')
%# draw circular path
line(D(:,1), D(:,2), 'Color',[.3 .3 .3], 'LineWidth',1);
%# initialize point
hLine = line('XData',D(1,1), 'YData',D(1,2), 'EraseMode','xor', ...
'Color','r', 'marker','.', 'MarkerSize',50);
%# init text
hTxt = text(0, 0, num2str(t(1)), 'FontSize',12, 'EraseMode','xor');
i=0;
animation = false;
hBeginButton = uicontrol('Parent',hFig, 'Position',[1 1 30 20], ...
'String','<<', 'Callback',#beginButton_callback);
hPrevButton = uicontrol('Parent',hFig, 'Position',[30 1 30 20], ...
'String','<', 'Callback',#previousButton_callback);
hNextButton = uicontrol('Parent',hFig, 'Position',[60 1 30 20], ...
'String','>', 'Callback',#nextButton_callback);
hEndButton = uicontrol('Parent',hFig, 'Position',[90 1 30 20], ...
'String','>>', 'Callback',#endButton_callback);
hSlider = uicontrol('Parent',hFig, 'Style','slider', 'Value',1, 'Min',1,...
'Max',numel(t), 'SliderStep', [10 100]./numel(t), ...
'Position',[150 1 300 20], 'Callback',#slider_callback);
hPlayButton = uicontrol('Parent',hFig, 'Position',[500 1 30 20], ...
'String','|>', 'Callback',#playButton_callback);
hStopButton = uicontrol('Parent',hFig, 'Position',[530 1 30 20], ...
'String','#', 'Callback',#stopButton_callback);
%#----------- NESTED CALLBACK FUNCTIONS -----------------
function beginButton_callback(hObj,eventdata)
updateCircle(1)
end
function endButton_callback(hObj,eventdata)
updateCircle(numel(t))
end
function nextButton_callback(hObj,eventdata)
i = i+1;
if ( i > numel(t) ), i = 1; end
updateCircle(i)
end
function previousButton_callback(hObj,eventdata)
i = i-1;
if ( i < 1 ), i = numel(t); end
updateCircle(i)
end
function slider_callback(hObj, eventdata)
i = round( get(gcbo,'Value') );
updateCircle(i)
end
function playButton_callback(hObj, eventdata)
animation = true;
while animation
i = i+1;
if ( i > numel(t) ), i = 1; end
updateCircle(i)
end
end
function stopButton_callback(hObj, eventdata)
animation = false;
end
function updateCircle(idx)
set(hSlider, 'Value', rem(idx-1,numel(t))+1) %# update slider to match
set(hLine,'XData',D(idx,1), 'YData',D(idx,2)) %# update X/Y data
set(hTxt,'String',num2str(t(idx))) %# update angle text
drawnow %# force refresh
if ~ishandle(hAx), return; end %# check valid handle
end
%#-------------------------------------------------------
end
You might find the slider functionality a bit buggy, but you get the idea :)

Related

error bad argument #1 to draw (drawable expected, got table), trying to animate a sprite for the main character

new to Lua and love. I'm trying to animate a character for my platform game. I know my key binds and all work but I have never animated before, so I don't even know if I'm going in the right direction or not. I've tried looking up guides so I can compare my code to someone else's but it seems that most of them are outdated. Originally my player was just a white rectangle but now im trying to actually give it a sprite.
Here's my main.lua
local STI = require("sti")
local anim8 = require('anim8')
require("MC")
function love.load()
map1 = STI("map/map1.lua", {"box2d"})
Physics = love.physics.newWorld(0,0)
Physics:setCallbacks(beginContact, endContact)
map1:box2d_init(Physics)
map1.layers.Solid.visible = false
background = love.graphics.newImage("assets/Base pack/bg.png")
mc:load()
end
function love.update(dt)
Physics:update(dt)
mc:update(dt)
end
function love.draw()
love.graphics.push()
love.graphics.scale(5,3)
love.graphics.draw(background)
love.graphics.pop()
map1:draw(0, 0, 2, 2)
love.graphics.push()
love.graphics.scale(2,2)
mc:draw()
love.graphics.pop()
end
function love.keypressed(key)
mc:jump(key)
if keypressed == "escape" then
love.event.quit(0)
end
end
function beginContact(a, b, collision)
mc:beginContact(a, b, collision)
end
function endContact(a, b, collision)
mc:endContact(a, b, collision)
end
I don't believe that there is anything wrong with my main but i think it's necessary to recreate the problem (sorry if it makes this too long). Now my mc.lua is where I'm having the problem since im trying to animate the player themselves by giving them a jump idle and walk animation, I think i will give them a damaged and death animation later.
local anim8 = require('anim8')
mc = {}
spr_mc_walk = love.graphics.newImage("assets/mc sprites/1 Pink_Monster/Pink_Monster_Walk_6.png")
local w = anim8.newGrid(32, 32, spr_mc_walk:getWidth(), spr_mc_walk:getHeight())
walk = anim8.newAnimation(w('1-6', 1), 0.1)
spr_mc_idle = love.graphics.newImage("assets/mc sprites/1 Pink_Monster/Pink_Monster_Idle_4.png")
local i = anim8.newGrid(32, 32, spr_mc_idle:getWidth(), spr_mc_idle:getHeight())
idle = anim8.newAnimation(i('1-4', 1), 0.1)
spr_mc_jump = love.graphics.newImage("assets/mc sprites/1 Pink_Monster/Pink_Monster_Jump_8.png")
local j = anim8.newGrid(32, 32, spr_mc_jump:getWidth(), spr_mc_jump:getHeight())
jump = anim8.newAnimation(j('1-8', 1), 0.1)
obj_mc = walk
function mc:load()
self.x = 100
self.y = 0
self.width = 20
self.height = 60
self.xvel = 0
self.yvel = 0
self.maxspeed = 200
self.acceleration = 4000 -- max speed
self.friction = 3900 -- how long it takes them to reach max speed
self.gravity = 1000
self.jumpAmount = -500
self.grounded = false
self.physics = {}
self.physics.body = love.physics.newBody(Physics, self.x, self.y, "dynamic")
self.physics.body:setFixedRotation(true)
self.physics.shape = love.physics.newRectangleShape(self.width, self.height)
self.physics.fixture = love.physics.newFixture(self.physics.body, self.physics.shape)
end
function mc:draw()
love.graphics.draw(obj_mc, self.x, self.y)
end
This is most of my mc or player function, except for movement and such but this has all the code that I'm confused with. I'm pretty sure I wrote the code right I'm just confused on how to actually implement it now that I have it written down. I thought just doing the draw function would actually draw the idle version of the character but then there's where I get the error. If you need anymore code or anymore details just let me know.
The error that this is happening at is
Error
MC.lua:41: bad argument #1 to 'draw' (Drawable expected, got table)
Traceback
[C]: in function 'draw'
MC.lua:41: in function 'draw'
main.lua:30: in function 'draw'
[C]: in function 'xpcall'
I haven't done any of that for a while, but I believe obj_mc, in this case, is a table of the frames. You need to loop through them during update.
local anim8timer = 0
local anim8index = 1
local anim8rate = 0.2 -- pick an animation rate that looks good for your game
function mc:update( dt )
anim8timer = anim8timer +dt -- add delta time
if anim8timer >= anim8rate then
anim8timer = anim8timer -anim8rate -- reset timer
anim8index = anim8index +1 -- next frame
if anim8index > #obj_mc then anim8index = 1 end -- loop frames
end
end
function mc:draw()
love.graphics.draw( obj_mc[ anim8index ], self.x, self.y )
end
They may have a built-in function that accomplishes some of that, so look for anything that resembles this in the Love2D forum.

How do I make a script affect all its children in Roblox LUA?

I'm new to programming in LUA, although I've learned similar languages like JS. It's frustrating if I have to alter the same script in many parts in a group by replacing each script, and I don't know of an elegant way to do it. Instead, I decided to nest all the parts inside of the script. I've seen some examples and I've tried to adapt some of them, but they don't exactly apply to what I want to do and I can't get them to work.
In essence, what I'm trying to do is monitor all the bricks for a player to contact them. I took the original disappearing brick script that was nested inside each brick and modified it. If a part (brick) is touched, that should call the onTouch function, which will make the brick's transparency decrease over time until the in pairs loop is done, after which the brick disappears and CanCollide is turned off. After 2 seconds, it then returns back to normal. I think the problem is with the coding I used to monitor the parts as I don't really understand the right way to monitor multiple objects. Can someone please help? Thanks!
File structure:
function onTouched(brick)
local delay = .1 -- the delay between each increase in transparency (affects speed of disappearance)
local RestoreDelay = 2 -- delay before the brick reappears
local inc = .1 -- how much the brick disappears each time
-- All characters have a Humanoid object
-- if the model has one, it is a character
local h = script.Child:findFirstChild("Humanoid") -- Find Humanoids in whatever touched this
if (h ~=nil) then -- If there is a Humanoid then
h.Health = h.MaxHealth -- Set the health to maximum (full healing)
for x=0,1, inc do
script.Child.Transparency = x+inc
script.Child.CanCollide = true
wait(delay)
end
wait(delay)
script.Child.Transparency = 1
script.Child.CanCollide = false
wait(RestoreDelay)
script.Child.Transparency = 0
script.Child.CanCollide = true
else
end
end
while true do
local bricks=script:GetChildren():IsA("basic.part")
for x=1,brick in pairs(bricks) do
brick.Touched:connect(onTouched(brick)) -- Make it call onTouched when touched
end
end
end
For the most part, you've gotten it right, but you've got a few syntax errors where there are different conventions between JavaScript and Lua.
In JS, you would fetch an array of objects and then bee able to filter it immediately, but in Lua, there is limited support for that. So a JavaScript line like :
var bricks = script.GetChildren().filter(function(item) {
return item === "basic.part"
})
cannot be done all in one line in Lua without assistance from some library. So you'll need to move the check into the loop as you iterate over the objects.
Other than that, the only other thing to change is the onTouched handler's function signature. The BasePart.Touched event tells you which object has touched the brick, not the brick itself. But by creating a higher order function, it's easy to get access to the brick, and the thing that touched it.
-- create a helper function to access the brick and the thing that touched it
function createOnTouched(brick)
-- keep track whether the animation is running
local isFading = false
return function(otherPart)
-- do not do the animation again if it has already started
if isFading then
return
end
local delay = .1 -- the delay between each increase in transparency (affects speed of disappearance)
local restoreDelay = 2 -- delay before the brick reappears
local inc = .1 -- how much the brick disappears each time
-- All characters have a Humanoid object, check for one
local h = otherPart.Parent:FindFirstChild("Humanoid")
if h then
-- heal the player
h.Health = h.MaxHealth
-- start fading the brick
isFading = true
brick.CanCollide = true
for i = 0, 1, inc do
brick.Transparency = i
wait(delay)
end
-- turn off collision for the brick
wait(delay)
brick.Transparency = 1
brick.Anchored = true
brick.CanCollide = false
-- turn the part back on
wait(restoreDelay)
brick.Transparency = 0
brick.CanCollide = true
-- reset the animation flag
isFading = false
end
end
end
-- loop over the children and connect touch events
local bricks = script:GetChildren()
for i, brick in ipairs(bricks) do
if brick:IsA("BasePart") then
local onTouchedFunc = createOnTouched(brick)
brick.Touched:Connect(onTouchedFunc)
end
end

Call a function in MATLAB GUIDE gui

I have a script called 'main.m' that basically takes the paths where I've saved all my images and insert them in arrays. It saves the images name in a .dat file and call a function named 'selectFolder.m'.
I posted all the script and functions under, my request is at the bottom.
%% Folders
imgFolder = './1.Dataset/';
functFolder = './2.Functions/' ;
%resFolder = './3.Results/';
%% Add path
addpath(genpath(imgFolder));
addpath(genpath(functFolder));
%% Listing Folders where my images are at
myFolder1 = '../Always'; %folder path
[..] %12 folders in total
myFolder12 = '../Random'; %folder path
%% Distinguish folder 'Always' & 'Random'
% Always Folders: subset of images for all users
mfA = {myFolder1, myFolder3, myFolder5, myFolder7, myFolder9, myFolder11};
dimA = length(mfA);
% Random Folders: subset of images randomly showed
mfR = {myFolder2, myFolder4, myFolder6, myFolder8, myFolder10, myFolder12};
dimR = length(mfR);
% check if folders are present
for i = 1:dimA
if ~isdir(mfA{i})
errorMessage = sprintf('Error: The following folder does not exist:\n%s', mfA{i});
uiwait(warndlg(errorMessage));
return;
end
end
for j = 1:dimR
if ~isdir(mfR{j})
errorMessage = sprintf('Error: The following folder does not exist:\n%s', mfR{j});
uiwait(warndlg(errorMessage));
return;
end
end
%% Take images and insert'em in Arrays
% Always
MyImgs1 = dir(fullfile(mfA{1}, '*.jpg'));
[..] %for every cell
MyImgs6 = dir(fullfile(mfA{6}, '*.jpg'));
% Random
MyImgs1r = dir(fullfile(mfR{1}, '*.jpg'));
[..] %for every cell
MyImgs6r = dir(fullfile(mfR{6}, '*.jpg'));
% create arrays with images names
Array_mfA = {MyImgs1.name, MyImgs2.name, MyImgs3.name, MyImgs4.name, MyImgs5.name, MyImgs6.name};
Array_mfR = {MyImgs1r.name, MyImgs2r.name, MyImgs3r.name, MyImgs4r.name, MyImgs5r.name, MyImgs6r.name};
%% Print content of array on file
fileIDA = fopen('2.Functions/Array_Always.dat','w');
formatSpec = '%s,';
nrows = length(Array_mfA);
for row = 1 : nrows
fprintf(fileIDA, formatSpec, Array_mfA{row});
end
fclose(fileIDA);
fileIDR = fopen('2.Functions/Array_Random.dat','w');
formatSpec = '%s,';
nrows = length(Array_mfR);
for row = 1 : nrows
fprintf(fileIDR, formatSpec, Array_mfR{row});
end
fclose(fileIDR);
%disclaimer
nrc = 1;
file = fopen('2.Functions/disclaimer.dat', 'w');
fprintf(file, '%d', nrc);
fclose(file);
%% call function
selectFolder(mfA, mfR);
This function takes two array as input, these array contains all the names of my images sorted. It does some operation and then it calls another function 'selectImage.m' that displays fullscreen the selected image.
function [] = selectFolder(mfA, mfR)
clc
%% Open Arrays from file
% Always
fileID = fopen('2.Functions/Array_Always.dat', 'rt');
Array_A = textscan(fileID,'%s', 'Delimiter', ',');
fclose(fileID);
% Random
fileID2 = fopen('2.Functions/Array_Random.dat', 'rt');
Array_R = textscan(fileID2,'%s', 'Delimiter', ',');
fclose(fileID2);
%% Show Disclaimer
file = fopen('2.Functions/disclaimer.dat', 'r');
dis = fscanf(file, '%d');
fclose(file);
if (dis == 1)
set(gcf,'Toolbar','none','Menubar','none', 'NumberTitle','off');
set(gcf,'units','normalized','outerposition',[0 0 1 1]);
hAx = gca;
set(hAx,'Unit','normalized','Position',[0 0 1 1]);
imshow('1.Dataset/Disclaimer/DIS.jpg');
drawnow;
nrc = 0;
file = fopen('2.Functions/disclaimer.dat', 'w');
fprintf(file, '%d', nrc);
fclose(file);
return;
end
%% select random folder from 'Array_A' aka Always Array
dimA = length(mfA);
if ~isempty(Array_A{1})
rndn = randperm(dimA, 1);
A_check = Array_A;
while isequal(A_check,Array_A)
Array_A = selectImage(mfA{rndn}, Array_A);
if isequal(A_check,Array_A)
rndn = randperm(dimA, 1);
end
end
fileIDA = fopen('2.Functions/Array_Always.dat','w');
formatSpec = '%s,';
nrows = cellfun('length', Array_A);
for row = 1 : nrows
fprintf(fileIDA, formatSpec, Array_A{1}{row});
end
fclose(fileIDA);
return;
end
%% select random folder from 'Array_R' aka Random Array
if ~isempty(Array_R{1})
dimR = length(mfR);
rndnr = randperm(dimR, 1);
R_check = Array_R;
while isequal(R_check,Array_R)
Array_R = selectImage(mfR{rndnr}, Array_R);
if isequal(R_check, Array_R)
rndnr = randperm(dimR, 1);
end
end
fileIDR = fopen('2.Functions/Array_Random.dat','w');
formatSpec = '%s,';
nrows = cellfun('length', Array_R);
for row = 1 : nrows
fprintf(fileIDR, formatSpec, Array_R{1}{row});
end
fclose(fileIDR);
end
end
selectImage:
function [ Array ] = selectImage( myFolder, Array )
%% Check
MyImgs = dir(fullfile(myFolder, '*.jpg'));
dim = length(MyImgs);
n = 0;
for i = 1 : dim
MyImgs(i).name
if ~any(strcmp(Array{1}, MyImgs(i).name))
disp(MyImgs(i).name);disp('not present in ');disp(myFolder);
n = n + 1;
end
end
if (n == dim)
disp('empty folder')
return;
end
rN = randperm(dim, 1);
baseFileName = MyImgs(rN).name;
while ~any(strcmp(Array{1}, baseFileName))
fprintf(1, 'not present %s\n', baseFileName);
rN = randperm(dim, 1);
baseFileName = MyImgs(rN).name;
end
%% Dispay image
dim = cellfun('length', Array);
for i = 1 : dim
if strcmp(baseFileName, Array{1}(i))
Array{1}(i) = [];
break
end
end
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
set(gcf,'Toolbar','none','Menubar','none', 'NumberTitle','off');
set(gcf,'units','normalized','outerposition',[0 0 1 1]);
hAx = gca;
set(hAx,'Unit','normalized','Position',[0 0 1 1]);
imshow(imageArray); % Display image.
drawnow;
end
Now I have to integrate these functions in my gui. What I want to do is call the 'main.m' script just one time with a button like 'Let's Start' and with that will show the disclaimer.
Then repeat the process calling only the 'Next' button, which calls 'selectFolder.m' and display the images with the procedure described above.
Is it possibile to do it this way? I mean, how can I pass the variable 'mfA' and 'mfR' to selectFolder? Is there a better and simpler way to do it?
The code in the gui is like:
-main:
% --- Executes on button press in Start.
function Start_Callback(hObject, eventdata, handles)
% hObject handle to Start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
figure
main
-selectFolder:
function Next_Callback(hObject, eventdata, handles)
% hObject handle to Next (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
figure %show the image in another window
selectFolder(mfA, mfR)
An easy way to share variables among the callback of a GUI is to use the
guidata function.
With respect to your specific variables mfA and mfR you can use guidata
to store them, this way: in the callback in which you generate the variables you want to share with other callback you can insert the following code:
% Get the GUI data
my_guidata=guidata(gcf);
%
% section of your code in which you create the mfA and mfR vars
%
% Store the variables to be shared among the callbacks in the guidata
my_guidata.mfA=mfA;
my_guidata.mfR=mfR;
% Save the updated GUI data
guidata(gcf,my_guidata);
In the callback in which you wnat to retreive the data, you can insert the
following code:
% Get the GUI data
my_guidata=guidata(gcf);
% Retrieve the data from the GUI data structure
mfA=my_guidata.mfA;
mfR=my_guidata.mfR;
In both the examples, the struct my_guidata holds the handles of the GUI and the additional varaibles you have defined.
With respect to the the architecture of the GUI, there are lots of possibilities.
Firt a comment: looking at the two callback you've posted at the bottom of your question, it seems that your GUI has, at least, one axes, nevertheless, you create, in both of them a new figure so it is not clear the role of that axes
Considering now your questions
What I want to do is call the 'main.m' script just one time with a button like 'Let's Start' and with that will show the disclaimer. Then repeat the process calling only the 'Next' button, which calls 'selectFolder.m' and display the images with the procedure described above
call the 'main.m' script just one time with a button like 'Let's Start' and with that will show the disclaimer
You have just to copy the relevant code of your main in the Start pushbutton callback.
Notice that the code which shows the disclaimer is actually in your selectFolder function, so you have to move it in the
Start callback.
Then repeat the process calling only the 'Next' button, which calls 'selectFolder.m' and display the images with the procedure described above
to do this, you have to remove the call to selectFolder from the main and move the body of your in the Next pushbotton callback.
Also you can copy the selectImage in the GUI .m file.
Hope this helps.
Qapla'

how to set the scale to logarithmic and linear when i right click on the particular figure in GUI matlab

I have created a Gui in matlab which has three axes and i would like to change the scale to log and linear by right click on the particular figure and select the scale options(1.logarithmic 2.linear). I tried using the UIcontextmenu. But i didn't get the result.
firstcycle_mass = handles.Data1{1,1}(1:6392,1);
firstcycle_ioncurrent = handles.Data1{1,2}(1:6392,1);
replace_with_dot_firstcycle_mass = strrep(firstcycle_mass, ',','.');
X1 = str2double(replace_with_dot_firstcycle_mass);
replace_with_dot_firstcycle_ioncurrent = strrep(firstcycle_ioncurrent, ',','.');
Y1 = str2double(replace_with_dot_firstcycle_ioncurrent);
axes(handles.axes1);
plotline= plot(X1,Y1,'r');
xlabel('Mass [amu]');
ylabel('Ion current [A]');
c=uicontextmenu;
plotline.UIContextMenu = c;
% Create menu items for the uicontextmenu
m1 = uimenu(c,'Label','logarithmic','Callback',#setlinestyle);
m2 = uimenu(c,'Label','linear','Callback',#setlinestyle);
function setlinestyle(source,callbackdata)
switch source.Label
case 'logarithmic'
set(gca,'yscale','log')
case 'linear'
set(gca,'yscale','linear')
end
end

How do I randomly select an object from a table in Lua?

I'm trying to add a function that randomly selects objects from the table targets. I read somewhere that you can use targets[math.random(#targets)], but when I do that, it doesn't just reset one of the targets regardless of resetTarget() call, and it doesn't actually make the next target random.
local targets -- an array of target objects
local bomb = display.newImage("bomb.png")
local asteroid = display.newImage("asteroid.png")
local balloon = display.newImage("balloon.png")
targets = { bomb, asteroid, balloon }
function createTarget()
for i = 1, #targets do
local t = targets[i]
t.x = WIDTH + 50 -- start slightly off screen to the right
t.y = math.random(100, HEIGHT - 100) -- at random altitude
end
end
function resetTarget(obj)
createTarget()
end
function detectHits()
-- Do hit detection for ball against each target
local HIT_SLOP = BIRD_RADIUS * 2 -- Adjust this to adjust game difficulty
for i = 1, #targets do
local t = targets[i]
if math.abs(t.x - bird.x) <= HIT_SLOP
and math.abs(t.y - bird.y) <= HIT_SLOP then
-- Hit
isBomb(t)
isAsteroid(t)
isBalloon(t)
resetTarget(t)
updateScore()
end
end
end
This will work but you will need a forward reference to currentTarget.
What is your function to target the random target?
local newTarget = function()
local rand = math.random(1,#targets)
currentTarget = target[rand]
doSomething()
end

Resources